如何迭代多维数组?

时间:2013-04-03 14:55:11

标签: php loops multidimensional-array foreach iteration

我有这样的数组。此数组是动态创建的

 Array
 (
  [Data NotUploading] => Array
    (
        [items] => Array
            (
                [0] => Array
                    (
                        [date] => 2013-04-02
                        [issue_id] => 1
                        [phone_service_device] => A
                        [phone_service_model] => 
                        [phone_service_os] => 
                        [phone_service_version] => 
                        [issue_name] => Data NotUploading
                    )

                [1] => Array
                    (
                        [date] => 2013-04-02
                        [issue_id] => 1
                        [phone_service_device] => I
                        [phone_service_model] => 
                        [phone_service_os] => 
                        [phone_service_version] => 
                        [issue_name] => Data NotUploading
                    )



            )

    )

[Battery Problem] => Array
    (
        [items] => Array
            (
                [0] => Array
                    (
                        [date] => 2013-04-03
                        [issue_id] => 3
                        [phone_service_device] => I
                        [phone_service_model] => 
                        [phone_service_os] => 
                        [phone_service_version] => 
                        [issue_name] => Battery Problem
                    )

            )

    )
)

我需要做的是使用2 foreach或1 foreach& 1 for循环,这样我就可以获得我喜欢的每个日期值

 foreach($gResultbyName  as $key1 => $rec){
         for($j = 0;$j<count($rec);$j++ ){
             echo $rec['items'][$j]['date']; 
         }
     }

但它唯一的检索2013-04-02&amp; 2013-04-03即数据的0索引日期NotUploading&amp; 0电池问题的索引日期。基本上我需要比较每个值和其他东西,但我不能得到每个日期值。

请原谅我无知,但我尝试了很多:(

3 个答案:

答案 0 :(得分:3)

试试这个:

foreach ($data as $d)
{
    foreach($d['items'] as $item)
    {
        echo $item['date'];
    }
}

答案 1 :(得分:2)

你的for循环循环count($rec)次,但count($rec['items'])

答案 2 :(得分:2)

这应该将日期加载到按issue_name排序的数组中。您可以单步执行它们以进行进一步处理。

$dates= array();
foreach ($gResultbyName AS $events){
    foreach ($events['items'] AS $item){
        $dates[$item['issue_name']][] = $item['date'];
    }
}
var_dump($dates);