php 多维数组获取值

这是我在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价格我已经尝试了几个前循环,但无法获得结果

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

此代码不起作用。


答案 1

这是迭代此数组的方法:

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

您希望迭代酒店和房间(带有数字索引的那些),因为在本例中,这些似乎是“集合”。其他数组仅保存和分组属性。


答案 2

对于那些搜索并真正想要解决问题的人来说,来自从2维数组中获取一列值(像我一样!),这里有一种比使用更优雅的方式,这是array_columnphp multidimensional array get valuesforeach

例如,如果我只想从下面的数组中获取,并形成另一个数组:hotel_name

$hotels = [
    [
        'hotel_name' => 'Hotel A',
        'info' => 'Hotel A Info',
    ],
    [
        'hotel_name' => 'Hotel B',
        'info' => 'Hotel B Info',
    ]
];

我可以使用以下命令执行此操作:array_column

$hotel_name = array_column($hotels, 'hotel_name');

print_r($hotel_name); // Which will give me ['Hotel A', 'Hotel B']

对于这个问题的实际答案,它也可以通过call_user_func_array(“array_merge”,$twoDimensionalArray) 来美化和call_user_func_array;array_column

让我们在PHP中制作数据:

$hotels = [
    [
        'hotel_name' => 'Hotel A',
        'info' => 'Hotel A Info',
        'rooms' => [
            [
                'room_name' => 'Luxury Room',
                'bed' => 2,
                'boards' => [
                    'board_id' => 1,
                    'price' => 200
                ]
            ],
            [
                'room_name' => 'Non Luxy Room',
                'bed' => 4,
                'boards' => [
                    'board_id' => 2,
                    'price' => 150
                ]
            ],
        ]
    ],
    [
        'hotel_name' => 'Hotel B',
        'info' => 'Hotel B Info',
        'rooms' => [
            [
                'room_name' => 'Luxury Room',
                'bed' => 2,
                'boards' => [
                    'board_id' => 3,
                    'price' => 900
                ]
            ],
            [
                'room_name' => 'Non Luxy Room',
                'bed' => 4,
                'boards' => [
                    'board_id' => 4,
                    'price' => 300
                ]
            ],
        ]
    ]
];

计算结果如下:

$rooms = array_column($hotels, 'rooms');
$rooms = call_user_func_array('array_merge', $rooms);
$boards = array_column($rooms, 'boards');

foreach($boards as $board){
    $board_id = $board['board_id'];
    $price = $board['price'];
    echo "Board ID is: ".$board_id." and price is: ".$price . "<br/>";
}

这将得到以下结果:

Board ID is: 1 and price is: 200
Board ID is: 2 and price is: 150
Board ID is: 3 and price is: 900
Board ID is: 4 and price is: 300