如何显示多维度数组第二层中的json值? [英] How to display json values that are in 2nd level of a multi-dim array?
本文介绍了如何显示多维度数组第二层中的json值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决此代码时遇到问题.这些是数组
Having a problem on solving this code. These are the arrays
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => delux
[price] => 213
[description] =>
[tv] => 0
[breakfast] => 0
[park] => 0
[wifi] => 0
[ac] => 0
[occupancy] =>
[size] =>
[view] =>
[service] =>
[terrace] => 0
[pickup] => 0
[internet] => 0
[gym] => 0
[note] =>
[room_details] => {"img":["images/logo2.png","images/logo.png"]}
)
[1] => stdClass Object
(
[id] => 2
[name] => hjghj
[price] => 234
[description] =>
[tv] => 0
[breakfast] => 0
[park] => 0
[wifi] => 0
[ac] => 0
[occupancy] =>
[size] =>
[view] =>
[service] =>
[terrace] => 0
[pickup] => 0
[internet] => 0
[gym] => 0
[note] =>
[room_details] =>
)
)
我想回显room_details下的每个图像,以使其显示为
I want to echo the per images under room_details to show like this
images/logo2.png
images/logo.png
images/logo2.png
images/logo.png
这是我的代码
foreach ($roomandsuits as $i => $item) {
$array_links = json_decode($item->room_details, true); {
foreach ($array_links as $key => $value) {
foreach ($value as $content) {
echo $content;
}
}
}
}
第三行出现错误,并显示如下
Error in 3rd line and shows like this
images/logo2.png
images/logo.png
警告:在第10行的C:\ xampp \ htdocs \ resort \ modules \ mod_roomandsuits \ tmpl \ default.php中为foreach()提供了无效的参数 images/logo.png
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\resort\modules\mod_roomandsuits\tmpl\default.php on line 10 images/logo.png
推荐答案
尝试以下代码.我在评论中添加了详细信息.
Try the below code. I added the details with comments.
foreach ($roomandsuits as $i => $item) {
if($item->room_details){ //check if value of $item->'room_details' not null
$room_details = json_decode($item->room_details, true); //decode the json data
if(!empty($room_details)){ //Check if room_details is not empty array
$room = $room_details['img'];
array_walk($room, function($value){ //using array_walk gate the value of room_details
echo $value .'<br/>';
});
}
}
}
这篇关于如何显示多维度数组第二层中的json值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文