如何从php多维数组中获取值?

时间:2016-02-02 06:40:27

标签: php arrays multidimensional-array

如何从此数组中获取数字和月份的值。

Array
(
   [mane] => Riya
   [id] => 70
   [order] => Array
     (
        [details] => Array
            (
                [number] => 4112
                [month] => March
            )

    )

这是我试过的代码

foreach($order as $row) {
         echo $row['details']['number'];
         echo $row['details']['month'];            
    }

3 个答案:

答案 0 :(得分:1)

您缺少order数组。

改变
echo $row['details']['number'];
echo $row['details']['month'];

echo $row['order']['details']['number'];
echo $row['order']['details']['month'];
//           ^ error was here.

答案 1 :(得分:1)

你可以这样做

foreach($order as $key => $value){
    if( is_array($value) and !empty($value) ){
        foreach($value as $k => $v){
            printData($v['number']);    
            printData($v['month']);
        }
    }else{
        printData($value);  
    }   
}

答案 2 :(得分:1)

这是一个简单的解决方案:

 foreach ($order['order'] as $key => $value)
{
    echo $value['number']."<BR />";
    echo $value['month']."<BR />";
}

以下是适合您的DEMO