问题描述
在我的一个项目中,我需要从输入json中提取每个phone
.杰森输入类似:
In my one project i required to extract each phone
from input json. Json input something like:
[
{
"name": "Niyo",
"email": "[email protected]",
"phone": "8989457845"
},
{
"name": "Picks",
"email": "[email protected]",
"phone": "7878121245"
},
{
"name": "Chintz",
"email": "[email protected]",
"phone": "8745421254"
},
{
"name": "Kabiru Wabyu",
"email": "[email protected]",
"phone": ""
},
{
"name": "Rons",
"email": "",
"phone": "9898989898"
}
]
我知道一种从输入json contact
I know one solutions to extract each phone from input json contact
foreach($contacts as $phone){ $phones[]=$phone->phone;}
foreach($contacts as $phone){ $phones[]=$phone->phone;}
php/laravel中是否有其他方法可以从输入json中获取特定键的所有值?
Is there any alternate way in php/laravel to get all values from input json for specific key ?
推荐答案
这将返回您要查找的内容,只要您的PHP 5.5或更高版本即可.不知道是否有必要将json_decode的第二个参数设置为TRUE,这将返回一个数组数组,而不是对象数组.
This should return what you're seeking, provided that you have php 5.5 or more recent. Not sure if it's necessary to set the second parameter of json_decode to TRUE, which returns an array of arrays, rather than array of objects.
$x = json_decode(yourjson, TRUE);
$phones = array_column($x, 'phone');
这篇关于从特定键的每个json对象获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!