如何使用php中的数组中的foreach数组?

时间:2013-09-24 09:09:53

标签: php

我在数组中有一个数组:

$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 个答案:

答案 0 :(得分:2)

它打印数组,因为$ myarray是数组,修改你的脚本如下:

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

答案 1 :(得分:1)

尝试

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

答案 2 :(得分:0)

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

$tmp_arr = array();
foreach ($tree as $parent_arr) {
  foreach($parent_arr as $key => $values) {
    $tmp_arr [$key] = $values;
  }
}
$tree = $tmp_arr;

答案 3 :(得分:0)

foreach($tree as $subtree) { 
    echo $subtree[$value]; 
}