Php 对象的递归遍历

Php 对象的递归遍历,php,recursion,Php,Recursion,我很难编写递归函数来遍历这个层次结构 object(stdClass)#290 (6) { ["category_id"]=> int(1) ["parent_id"]=> int(0) ["name"]=> string(4) "Root" ["position"]=> int(0) ["level"]=> int(0) ["

我很难编写递归函数来遍历这个层次结构

    object(stdClass)#290 (6) {
      ["category_id"]=>
      int(1)
      ["parent_id"]=>
      int(0)
      ["name"]=>
      string(4) "Root"
      ["position"]=>
      int(0)
      ["level"]=>
      int(0)
      ["children"]=>
      array(2) {
        [0]=>
        object(stdClass)#571 (7) {
          ["category_id"]=>
          int(2)
          ["parent_id"]=>
          int(1)
          ["name"]=>
          string(18) "Root MySite.com"
          ["is_active"]=>
          int(0)
          ["position"]=>
          int(0)
          ["level"]=>
          int(1)
          ["children"]=>
          array(11) {
            [0]=>
            object(stdClass)#570 (7) {
              ["category_id"]=>
              int(15)
              ["parent_id"]=>
              int(2)
              ["name"]=>
              string(9) "Widgets"
              ["is_active"]=>
              int(1)
              ["position"]=>
              int(68)
              ["level"]=>
              int(2)
              ["children"]=>
              array(19) {
                [0]=>
                object(stdClass)#566 (7) {
                  ["category_id"]=>
                  int(24)
                  ["parent_id"]=>
                  int(15)
                  ["name"]=>
                  string(16) "Blue widgets"
                  ["is_active"]=>
                  int(1)
                  ["position"]=>
                  int(68)
                  ["level"]=>
                  int(3)
                  ["children"]=>
                  array(0) {
                  }
                }

<snip....>
[编辑]:粘贴递归函数的起点,该函数将简单地“展平”阵列或对象。我想我可以修改它,以获得我正在寻找的数据结构,但还没有完全正确

    function array_flatten($array, $return) 
{


  // `foreach` can also iterate through object properties like this 
  foreach($array as $key => $value)
  {
    if(is_object($value))
    {
      // cast objects as an array
      $value = (array) $value;
    }
    if(is_array($value))
    {
      $return = array_flatten($value,$return);
    }
    else
    {
      if($value)
      {
        $return[] = $value;
      }
    }

  }
  return $return;
}

问题是,我不太清楚如何递归地构建我正在寻找的结构,或者可能有一种更优雅的php方法来实现这一点

我没有时间写一个有效的答案,但这里有一些伪代码(一半是PHP,一半是JS)

这将通过删除列表中每个元素的children属性来创建树的展开版本

$flattened = array();

function addElement(&$flattened, $list) {
    for ($element in $list) {
        $children = $element->children;
        delete $element->children;
        $flattened[] = $element;
        if ($children) {
            addElements($flattened, $children)
        }
    }
}
addElements($flattened, $treeHierarchy);
试试这个

function run($o) {
    $return = array();
    foreach ($o->children as $child) {
        $return[$child->name] = run($child);
    }

    return empty($return) ? null : $return;
}

深序对象或关联数组

namespace a_nsp
class object_utils{

   static function deep_order($IN, $desc = NULL) { // object or assoc_array
       $O = new \stdClass;
       $I = (array) $IN; 
       $keys = array_keys($I);
       $desc ? rsort($keys) : asort($keys);
       foreach ($keys as $k) {
           $v = $I[$k]; 
           //$v = json_decode($I[$k],1) ?: $I[$k]; // force conversion of json_string
           if (is_array($v) || is_object($v)) {
               $O->$k = self::deep_order($v, $desc);
           }
           else {
               $O->$k=$v;
           }
       }
    return $O; // object
    } 

}


$ordered\u obj=\a\u nsp\object\u utils::deep\u order($orig\u obj)

您考虑过JSON吗?嗯。。我没有,我最终需要传递Javascript,所以这可能是一个开始。如果你使用JSON,你也可以在你的对象上直接使用JSON_编码。关于你关于将值转换为数组的问题,我不明白你为什么要这么做。为什么_array()在这里不起作用?这是您创建的类对象吗?你能给这个类添加一个方法吗?类似于
getChildren()
可以递归调用它:函数getCategoryTree($tree){$return=array();foreach($tree->children as$child){if(count($child->children)>0)$return[$child->name]=$this->getCategoryTree($child);else$return[]=$child->name;}return empty($return)?NULL:$return;}--使最后一个子元素不是数组,只是一个不能工作的stringyeah nevermind。。。如何使最后一个条目不是空数组?
namespace a_nsp
class object_utils{

   static function deep_order($IN, $desc = NULL) { // object or assoc_array
       $O = new \stdClass;
       $I = (array) $IN; 
       $keys = array_keys($I);
       $desc ? rsort($keys) : asort($keys);
       foreach ($keys as $k) {
           $v = $I[$k]; 
           //$v = json_decode($I[$k],1) ?: $I[$k]; // force conversion of json_string
           if (is_array($v) || is_object($v)) {
               $O->$k = self::deep_order($v, $desc);
           }
           else {
               $O->$k=$v;
           }
       }
    return $O; // object
    } 

}