-1

我在数组中有一个数组:

$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 主数组获取内部数组的值?

4

4 回答 4

2

它打印 Array 因为 $myarray 是数组,请像这样修改您的脚本:

foreach ($tree as $myarray) {
   echo $myarray[$value];
   echo $myarray[$left];
   echo $myarray[$leftValue];
}
于 2013-09-24T09:15:49.783 回答
1

尝试

foreach ($tree as $outer) {
  foreach ($outer as $inner) {
    // here you are
  }
}
于 2013-09-24T09:14:26.140 回答
0

下面的代码肯定会为你工作

$tmp_arr = array();
foreach ($tree as $parent_arr) {
  foreach($parent_arr as $key => $values) {
    $tmp_arr [$key] = $values;
  }
}
$tree = $tmp_arr;
于 2013-09-24T09:13:24.077 回答
0
foreach($tree as $subtree) { 
    echo $subtree[$value]; 
}
于 2013-09-24T09:13:25.117 回答