如何从多维数组索引输出数据

时间:2013-10-03 06:35:24

标签: php multidimensional-array

我正在尝试从其中一个数组父组索引中获取值 通过使用foreach循环获得第一级'1006',但没有太多运气。我可以 在这里使用一点帮助从给定的数组中获取子数据或子数据。

Array ( 
    [1004] => Array ( 
        [0] => Array ( [od_id] => 1004 [pd_id] => 42 [pd_name] => Red Hat ) 
        [1] => Array ( [od_id] => 1004 [pd_id] => 33 [pd_name] => Green Hat ) 
        [2] => Array ( [od_id] => 1004 [pd_id] => 44 [pd_name] => Blue Hat )
     ) 
    [1006] => Array ( 
        [0] => Array ( [od_id] => 1006 [pd_id] => 33 [pd_name] => Green Hat ) 
        [1] => Array ( [od_id] => 1006 [pd_id] => 44 [pd_name] => Blue Hat )
        [2] => Array ( [od_id] => 1006 [pd_id] => 41 [pd_name] => Pink Hat )
     ) 
    [1007] => Array (
        [0] => Array ( [od_id] => 1007 [pd_id] => 29 [pd_name] => Purple Hat ) 
        [1] => Array ( [od_id] => 1007 [pd_id] => 24 [pd_name] => Orange Hat ) 
        [2] => Array ( [od_id] => 1007 [pd_id] => 44 [pd_name] => Blue Hat ) 
    )
)

3 个答案:

答案 0 :(得分:1)

我想你想要的东西如下: -

<?php
$s=array ( "1004" => array (     " 0" => array ( "od_id" => 1004 ,"pd_id"=> 42 ,"pd_name" => 'Red Hat' ) ,"1" => array ( "od_id" => 1004, "pd_id" => 33, "pd_name" => 'Green Hat' ),"2" => array ( "od_id" => 1004,"pd_id" => 44,"pd_name" => 'Blue Hat' )));
foreach($s as $a=>$b)
{
foreach($b as $c=>$d)
{
foreach($d as $e=>$f){
echo $f;
}
echo'<br>';
}
}
?>

答案 1 :(得分:0)

不确定我是否完全理解您的问题,但如果您只想阅读给定索引中的所有值,您可以执行以下操作:

foreach ($array[1006] as $arr) {
    foreach ($arr as $k=>$v) {
        echo "$k: $v ; ";
    }
    echo "\n";
}

将打印:

od_id: 1006 ; pd_id: 33 ; pd_name: Green Hat ;
od_id: 1006 ; pd_id: 44 ; pd_name: Blue Hat ;
...

答案 2 :(得分:0)

使用array_multisort()函数 更多细节read here