php 如何在laravel中解码Json对象并在laravel中对其应用foreach循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29070907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Decode Json object in laravel and apply foreach loop on that in laravel
提问by Parag Bhingre
i am getting this request. ??
我收到了这个请求。??
{ "area": [
{
"area": "kothrud"
},
{
"area": "katraj"
}
]
}
and i want to provide response to this by searching records in database based on above request. how will i decode above json array and use each area field separately.
我想通过根据上述请求在数据库中搜索记录来对此做出回应。我将如何解码上面的 json 数组并分别使用每个区域字段。
回答by itachi
your string is NOT a valid json to start with.
您的字符串不是有效的 json 开始。
a valid json will be,
一个有效的 json 将是,
{
"area": [
{
"area": "kothrud"
},
{
"area": "katraj"
}
]
}
if you do a json_decode
, it will yield,
如果你做 a json_decode
,它会产生,
stdClass Object
(
[area] => Array
(
[0] => stdClass Object
(
[area] => kothrud
)
[1] => stdClass Object
(
[area] => katraj
)
)
)
Update:to use
更新:使用
$string = '
{
"area": [
{
"area": "kothrud"
},
{
"area": "katraj"
}
]
}
';
$area = json_decode($string, true);
foreach($area['area'] as $i => $v)
{
echo $v['area'].'<br/>';
}
Output:
输出:
kothrud
katraj
Update #2:
更新#2:
for that true
:
为此true
:
When TRUE, returned objects will be converted into associative arrays. for more information, click here
当为 TRUE 时,返回的对象将被转换为关联数组。欲了解更多信息,请单击此处