何获得动态多数组

时间:2019-07-02 11:47:34

标签: php

数组结构:

array (
  'payee_id' => 2381,
  'items' => 
          array (
            300 => 
                array (
                    'item_id' => 300,
                    ' price' => 50,
                ),
            372 => 
                array (
                      'item_id' => 372,
                      'price' => 90,
                ),
          ),
) 

我正在尝试动态获取item_idprice

代码

foreach($data as $key => $value) {
    Log::debug($value[300]['price']);
}

使用上面的代码,我可以获取密钥300 price,但是我也需要获取密钥372

1 个答案:

答案 0 :(得分:1)

使用foreach()

的另一种解决方案
<?php
foreach($data as $key => $value) { // if `$data` equal to $data['items'] as you mentioned in your question `Log::debug($value[300]['price']);`
    foreach ($value as $finalValue) { // loop for inner array
        echo $finalValue['price']."<br/>"; // print price in new line
    }   
}
?>