2013-09-24 83 views
-1

我在數組中的數組:如何在數組中的foreach數組?

$tree = array(array($value => 7, $left => 2, $right => 3, $leftValue = 0), 
       array($value => 5, $left => 4, $right => "", $leftValue = 0), 
       array($value => 25, $left => 5, $right => 6, $leftValue = 0), 
       array($value => 3, $left => "", $right => "", $leftValue = 0), 
       array($value => 12, $left => "", $right => "", $leftValue = 0), 
       array($value => 17, $left => "", $right => "", $leftValue = 0)); 

怎麼做的foreach主陣列來獲得內部數組的值?

+0

'foreach($ tree as $ entry){echo $ entry [$ value],PHP_EOL; }' –

回答

2

它打印數組,因爲$ myArray的是數組,修改你的腳本是這樣的:

foreach ($tree as $myarray) { 
    echo $myarray[$value]; 
    echo $myarray[$left]; 
    echo $myarray[$leftValue]; 
} 
0

下面的代碼將肯定爲你工作

$tmp_arr = array(); 
foreach ($tree as $parent_arr) { 
    foreach($parent_arr as $key => $values) { 
    $tmp_arr [$key] = $values; 
    } 
} 
$tree = $tmp_arr; 
0
foreach($tree as $subtree) { 
    echo $subtree[$value]; 
} 
1

嘗試

foreach ($tree as $outer) { 
    foreach ($outer as $inner) { 
    // here you are 
    } 
}