25

这是我在 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_idprice我尝试了几个 foreach 循环但无法获得结果

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

这段代码不起作用。

4

2 回答 2

37

这是迭代这个数组的方法:

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

您想迭代酒店和房间(带有数字索引的房间),因为在这种情况下,它们似乎是“集合”。其他数组仅保存和分组属性。

于 2012-04-14T10:40:54.727 回答
18

对于搜索php multidimensional array get values并真正想要解决问题的人来自从 2 维数组中获取一列值(比如我!),这是一种比 using 更优雅的方式foreach,即array_column

例如,如果我只想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']

对于这个问题的实际答案,也可以通过array_columnand美化call_user_func_array('array_merge', $twoDimensionalArray);

让我们在 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
于 2019-04-12T08:21:23.467 回答