如何在laravel中解码Json对象并在laravel中对其应用foreach循环 [英] How to Decode Json object in laravel and apply foreach loop on that in laravel
本文介绍了如何在laravel中解码Json对象并在laravel中对其应用foreach循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我收到此请求.
{ "area": [
{
"area": "kothrud"
},
{
"area": "katraj"
}
]
}
,我想根据上述要求通过在数据库中搜索记录来提供对此的响应.我将如何解码上述json数组并分别使用每个区域字段.
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开头.
your string is NOT a valid json to start with.
有效的json将是
{
"area": [
{
"area": "kothrud"
},
{
"area": "katraj"
}
]
}
如果您执行json_decode
,它将产生
stdClass Object
(
[area] => Array
(
[0] => stdClass Object
(
[area] => kothrud
)
[1] => stdClass Object
(
[area] => katraj
)
)
)
更新:以使用
$string = '
{
"area": [
{
"area": "kothrud"
},
{
"area": "katraj"
}
]
}
';
$area = json_decode($string, true);
foreach($area['area'] as $i => $v)
{
echo $v['area'].'<br/>';
}
输出:
kothrud
katraj
更新#2:
针对该true
:
为TRUE时,返回的对象将转换为关联数组.有关更多信息,请 单击此处
这篇关于如何在laravel中解码Json对象并在laravel中对其应用foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文