2013-10-03 100 views
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) 
    ) 
) 
+0

我寧願有結果分配給像

$od_id[] .= 'the array results'
而不是字符串的新數組。感謝您的輸入。 –

回答

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 ; 
... 
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>'; 
} 
} 
?> 
0

用在array_multisort()進行更詳細的功能 read here