php多维数组获取值

时间:2012-04-14 10:38:18

标签: php arrays multidimensional-array foreach

这是我在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循环但无法获得结果

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

此代码无效。

2 个答案:

答案 0 :(得分:29)

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

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

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

答案 1 :(得分:1)

对于那些搜索php multidimensional array get values并真正想解决问题的人来说,它是从2维数组中获得一列值(像我一样!),这比使用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_columncall_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