PHP:foreach数组多维,无序HTML列表中的响应

时间:2017-11-20 15:26:18

标签: php arrays multidimensional-array

我有以下数组:

Array
(
[code] => 20171122_7_1_1_109_d28df45a4cf941ab82052d976ac97905_
[count] => 2
[results] => Array
    (
        [0] => Array
            (
                [code] => 1b326e_2332105365_0_0_2_1_0_1_2332105381_0_0-0-0.20171122_7_1_1_109_d28df45a4cf941ab82052d976ac97905_
                [destination_code] => 20dc9
                [hotel_code] => 1b326e
                [additional_info] => 
                [checkin] => 2017-11-22
                [checkout] => 2017-11-29
                [price] => 853.47
                [currency] => EUR
                [pay_at_hotel] => 
                [hotel_price] => 
                [hotel_currency] => 
                [meal_type] => RO
                [nonrefundable] => 
                [view] => 
                [rooms] => Array
                    (
                        [0] => Array
                            (
                                [pax] => Array
                                    (
                                        [children_ages] => Array
                                            (
                                            )

                                        [adult_quantity] => 1
                                    )

                                [room_category] => Standard
                                [room_description] => King
                                [nightly_prices] => Array
                                    (
                                        [2017-11-22] => 121.92
                                        [2017-11-23] => 121.92
                                        [2017-11-24] => 121.92
                                        [2017-11-25] => 121.92
                                        [2017-11-26] => 121.92
                                        [2017-11-27] => 121.92
                                        [2017-11-28] => 121.95
                                    )

                                [room_type] => SB
                            )

                    )

                [supports_cancellation] => 1
                [minimum_selling_price] => 
                [offer] => 1
                [policies] => Array
                    (
                    )

            )

    )

)

通过简单的foreach,我可以打印[room_category,[room_description]的数据,但我无法在无序的HTML列表中打印类型为[nightly_prices]的日期和价格:

  • 2017-11-22:121.92
  • 2017-11-23:121.92
  • 2017-11-24:121.92

等...

我使用的代码如下:

$data2 = json_decode($result2, true);
foreach($data2['results'] as $key=>$val){ 
echo $val['rooms'][0]['nightly_prices']
}

但是只运行我的数组。

2 个答案:

答案 0 :(得分:0)

这是一个数组,所以你必须循环它才能获得每个值:

$data2 = json_decode($result2, true);
 foreach($data2['results'] as $key=>$val){ 
   foreach($val['rooms'][0]['nightly_prices'] as $k => $v){
      echo $k.' '.$v;
   }
  }

答案 1 :(得分:0)

您不能将echo用于数组,而是需要使用print_r代替:

print_r($val['rooms'][0]['nightly_prices']);