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.