PHP多维数组中获取值 [英] php multidimensional array get values

查看:211
本文介绍了PHP多维数组中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在PHP数组的 $酒店

this is my array in php $hotels

Array
(
    [0] => Array
        (
        [hotel_name] => Name
        [info] => info
        [rooms] => Array
            (
                [0] => Array
                    (
                        [room_name] => name
                        [beds] => 2
                        [boards] => Array
                            (
                                [board_id] => 1
                                [price] =>200.00
                            )
                    )
                )
        )
)

我怎样才能获得的 board_id 价格我已经tryed几个foreach循环,但不能得到的结果。

how can i get board_id and price i have tryed few foreach loops but cant get the result

   foreach($hotels as $row)
    {
       foreach($row as $k)
       {
          foreach($k as $l){
             echo $l['board_id'];
             echo $l['price'];
          }
       }
}

这code没有工作,和它有点傻。

this code didnt work, and its kinda stupid

推荐答案

这是遍历数组的方式:

foreach($hotels as $row) {
       foreach($row['rooms'] as $k) {
             echo $k['boards']['board_id'];
             echo $k['boards']['price'];
       }
}

您想重复的酒店和房间(那些使用数字索引),因为那些似乎是在这种情况下,集合。其他阵列只能容纳和组属性。

You want to iterate on the hotels and the rooms (the ones with numeric indexes), because those seem to be the "collections" in this case. The other arrays only hold and group properties.

这篇关于PHP多维数组中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆