【问题标题】:I got Undefined index: reading recursively an array我得到了未定义的索引:递归读取数组
【发布时间】:2013-08-08 21:19:47
【问题描述】:

我要疯了,我不明白这是什么问题。

我有这个数组:

array(2) {
      [0]=>
      array(4) {
        ["id"]=>
        string(1) "1"
        ["parent_id"]=>
        NULL
        ["name"]=>
        string(7) "Events"
        ["children"]=>
        array(2) {
          [0]=>
          array(3) {
            ["id"]=>
            string(1) "2"
            ["parent_id"]=>
            string(1) "1"
            ["name"]=>
            string(9) "Concerts"
          }
        }
      }
      [1]=>
      array(4) {
        ["id"]=>
        string(1) "4"
        ["parent_id"]=>
        NULL
        ["name"]=>
        string(7) "Music"
        ["children"]=>
        array(3) {
          [0]=>
          array(3) {
            ["id"]=>
            string(1) "5"
            ["parent_id"]=>
            string(1) "4"
            ["name"]=>
            string(4) "Rock"
          }
        }
      }
    }

我尝试使用这个递归函数进行打印:

public function printTree($tree) {
    $result = "";
    if(!is_null($tree) && count($tree) > 0) {
        $result .= '<ul>';
        foreach($tree as $node) {
            $result .= '<li>Cat: '.$node['name'];
                $subtree = array($node['children']);
                $this->printTree($subtree);
            $result .= '</li>';
        }
        $result .= '</ul>';
    }
    return $result;
}

我收到“未定义索引:名称”错误。 我需要申报姓名吗?如何? 数组是否语法错误?

如果我评论递归调用

$subtree = array($node['children']);
$this->printTree($subtree);, 

那么$node['name'] 不是未定义的并且代码可以工作,但当然只有一层深度。

已解决:(谢谢大家!)

public function printTree($tree) {
    $result = "";
    if(is_array($tree) && count($tree) > 0) {
        $result .= '<ul>';
        foreach($tree as $node) {
            $result .= '<li>Cat: '.$node['name'];
                if (isset($node['children'])) { 
                    $result .= $this->printTree($node['children']);
                }
            $result .= '</li>';
        }
        $result .= '</ul>';
    }
    return $result;
}

【问题讨论】:

  • $result.= '&lt;li&gt;Cat: ' . $node['name']; 行之前使用var_dumpprint_r。它会告诉你 $node 的内容,你将能够看到它在哪里/为什么抛出 NOTICE(不是错误)。
  • 你能指出你得到未定义索引错误的代码行吗?
  • 如果我使用 var_dump 我得到未定义的索引 Notica,但 laravel 允许我做 dd(); $node 的内容是: array(4) { ["id"]=> string(1) "1" ["parent_id"]=> NULL ["name"]=> string(7) "事件" ["children"]=> array(2) { [0]=> array(3) { ["id"]=> string(1) "2" ["parent_id"]=> string(1) " 1" ["name"]=> string(9) "演唱会" } [1]=> array(3) { ["id"]=> string(1) "3" ["parent_id"]=> string( 1) "1" ["name"]=> string(10) "Parties" } } } and of $nodo['name'] string(7) "Events"
  • 通知行是:$result .= '
  • Cat: '.$nodo['name'];

标签: php arrays recursion laravel undefined


【解决方案1】:

您正在将$node['children'] 推送到另一个数组中。这样你就不会处理这个节点的子数组(稍后会有一个名字),但是你有另一层数组。

跳过这个数组层,删除它。

还要注意!is_null() 并不是一个很好的检查,如果您想将该变量用作数组。请检查 is_array(),因为字符串和其他标量值也会返回 count&gt;0 - 它们返回 1。只有 NULL 返回 count=0。

【讨论】:

  • 好的,这是真的,但现在我得到了 Undefined index: children。我直接将代码更改为: $this->printTree($nodo['children']); 像这样我没有新层但 $nodo['children'] 未定义在这条线上。 :(
  • 是的,因为您没有检查您是否真的有另一个级别的孩子。如果有,请仅再次调用该函数。 if (isset($node['children'])) { $this-&gt;printTree...
  • 斯文,你是个好人!有用。我正在尝试if ($node['children']) w/o isset,因为我知道它是一样的。谢谢大家!
【解决方案2】:

您必须将子节点的连续调用 printTree() 返回的结果附加到 $result。

$result .= $this->printTree($node['children']);

:)

【讨论】:

  • 是的,我刚才看到了。我来编辑帖子并写得很好。谢谢
【解决方案3】:

首先,您希望将 $subtree-stuff 包含在一个 if 条件中,以检查键“children”是否存在,因为就像现在一样,您已经创建了对 @987654321 的递归调用的无限循环@,但你只想这样做,如果键 'children' 存在。

其次,我猜你想在$this-&gt;printTree($subtree);前面加上$result .=,否则返回值将被简单地丢弃。

第三,不要做$subtree = array($node['children']);$node['children'] 已经是一个数组,所以这给数组增加了另一个层次,这使得递归中断。

所以,最终的函数应该是这样的:

public function printTree($tree) {
    $result = '';
    if(!is_null($tree) && count($tree) > 0) {
        $result .= '<ul>';
        foreach($tree as $node) {
            $result .= '<li>Cat: '.$node['name'];
            if (isset($node['children'])) {
                $subtree = $node['children'];
                $result .= $this->printTree($subtree);
            }
            $result .= '</li>';
        }
        $result .= '</ul>';
    }
    return $result;
}

编辑:哎呀,那里太慢了,其他人也发现了这里的三个问题:)

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签