php php多维数组获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10152920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:31:29  来源:igfitidea点击:

php multidimensional array get values

phparraysmultidimensional-arrayforeach

提问by Petro Popelyshko

This is my array in php $hotels

这是我在 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
                            )
                    )
                )
        )
)

How can I get board_idand priceI have tried few foreach loops but can't get the result

我如何获得board_idprice我已经尝试了几个 foreach 循环但无法获得结果

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

This code didn't work.

此代码不起作用。

回答by kapa

This is the way to iterate on this array:

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

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.

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

回答by Ng Sek Long

For people who searched for php multidimensional array get valuesand actually want to solve problem comes from getting one column value from a 2 dimensinal array (like me!), here's a much elegant way than using foreach, which is array_column

对于搜索php multidimensional array get values并真正想要解决问题的人来说,从二维数组中获取一列值(就像我一样!),这是一种比使用更优雅的方法foreach,即array_column

For example, if I only want to get hotel_namefrom the below array, and form to another array:

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

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

I can do this using array_column:

我可以使用array_column

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

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


For the actual answer for this question, it can also be beautified by array_columnand call_user_func_array('array_merge', $twoDimensionalArray);

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

Let's make the data in PHP:

让我们在 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
                ]
            ],
        ]
    ]
];

And here's the calculation:

这是计算:

$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/>";
}

Which will give you the following result:

这会给你以下结果:

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