Php 从内到外遍历混合变量

Php 从内到外遍历混合变量,php,recursion,Php,Recursion,我在PHP中有以下对象/数组结构: Object ( id => *random number 10000-99999* name => parent => *reference to the parent Object* // null, so this is the root element child => Array ( [0] => Object (

我在PHP中有以下对象/数组结构:

Object ( id => *random number 10000-99999* name => parent => *reference to the parent Object* // null, so this is the root element child => Array ( [0] => Object ( id => *random number 10000-99999* name => row parent => *reference to the parent Object* // points to root element child => Array ( [0] => Object ( id => *random number 10000-99999* name => column parent => *reference to the parent Object* //row child => Array ( [0] => Object ( id => *random number 10000-99999* name => link parent => *reference to the parent Object* // column child => Array () ) ) ) [1] => Object ( id => *random number 10000-99999* name => column2 parent => *reference to the parent Object* //row child => Array ( [0] => Object ( id = *random number 10000-99999* name = article parent => *reference to the parent Object* // column2 child => Array () ) ) ) [2] => Object ( id = *random number 10000-99999* name = column3 parent => *reference to the parent Object* //row child => Array () ) ) ) [1] => Object ( id = *random number 10000-99999* name = link2 parent => *reference to the parent Object* // points to root element child => Array () ) ) ) 对象 ( id=>*随机数10000-99999* 名称=> parent=>*对父对象的引用*//null,因此这是根元素 child=>数组 ( [0]=>对象 ( id=>*随机数10000-99999* 名称=>行 parent=>*对父对象的引用*//指向根元素 child=>数组 ( [0]=>对象 ( id=>*随机数10000-99999* 名称=>列 父对象=>*对父对象的引用*//行 child=>数组 ( [0]=>对象 ( id=>*随机数10000-99999* 名称=>链接 parent=>*对父对象*//列的引用 child=>Array() ) ) ) [1] =>对象 ( id=>*随机数10000-99999* name=>column2 父对象=>*对父对象的引用*//行 child=>数组 ( [0]=>对象 ( id=*随机数10000-99999* 名称=文章 parent=>*对父对象的引用*//column2 child=>Array() ) ) ) [2] =>对象 ( id=*随机数10000-99999* name=column3 父对象=>*对父对象的引用*//行 child=>Array() ) ) ) [1] =>对象 ( id=*随机数10000-99999* name=link2 parent=>*对父对象的引用*//指向根元素 child=>Array() ) ) ) 我想从最深的对象处理这个变量。进进出出。像这样:

  • 链接
  • 纵队
  • 文章
  • 专栏2
  • 第3栏
  • 划船
  • 链接

  • 有人有好的解决办法吗?我可以为此编写合适的算法。我只想知道是否有人有一个经过验证的解决方案。

    无论是什么解决方案,您可能需要迭代到数组中以找到最深的“名称”,然后再迭代回来。您可以通过存储第一次迭代来最小化开销,并在找到最深的迭代时自动将其反转


    抱歉,午餐即将到来,因此没有时间提供一个工作示例;)

    递归算法将是您的朋友。。至少是最简单的实现。这不是问题,我只是希望有人会面临同样的问题。