迭代复杂的多维数组

时间:2013-08-30 12:47:12

标签: php arrays multidimensional-array

这是一些复杂的多维数组的输出:

    Array ( 
           [0] => Array ( 
                       [@attributes] => Array ( [ID] => 114037469 [Name] => MJBDESIGN.ORG [Traffic] => 4 [BidCount] => 0 [Price] => $8 USD [ValuationPrice] => - [TimeLeft] => 3H 27M [RowID] => 1 ) 
           ) 
           [1] => Array ( 
                       [@attributes] => Array ( [ID] => 114136929 [Name] => TRAININGPROGRAMMEDESIGN.COM [Traffic] => 11 [BidCount] => 0 [Price] => $8 USD [ValuationPrice] => $3 USD [TimeLeft] => 3H 27M [RowID] => 2 ) 
           ) 
)

如何遍历每个项目并获取单个项目的“ID”,“名称”等?谢谢

5 个答案:

答案 0 :(得分:5)

这个数组并不复杂。

$simpleArray = __YOUR_COMPLEX_ARRAY__;
foreach ($simpleArray as $array) {
    $id = $array['@attributes']['ID'];
    $name = $array['@attributes']['Name'];
}

答案 1 :(得分:1)

foreach ($array as $r) {
  $name = $r['@attributes']['Name'];
  $id = $r['@attributes']['ID'];
  # code ....
}

答案 2 :(得分:1)

如果您想要每行的所有属性:

foreach ($array as $n=>$r){
    echo "Row {$n}\n";
    foreach ($r as $k=>$v){
        // Echo the data
        echo "{$k}=>${v}\n";
    }
}

答案 3 :(得分:1)

很简单:

$array = // your long array;

foreach($array as $row)
{
    $id = $row['@attributes']['Id'];
    $name = $row['attributes']['Name'];
    // do something with these variables
}

答案 4 :(得分:0)

试试吧,

foreach ($yourarray as $val) {
    echo $id = $val['@attributes']['ID'];
    echo $name = $val['@attributes']['Name'];
}