0

我正在尝试通过使用 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 ) 
    )
)
4

3 回答 3

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>';
}
}
?>
于 2013-10-03T07:02:07.583 回答
0

使用 array_multisort() 函数了解更多详细信息,请阅读此处

于 2013-10-03T07:05:00.507 回答
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 ;
...
于 2013-10-03T06:43:06.907 回答