4

この階層構造をトラバースする再帰関数を書くのに問題があります

    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....>

ご覧のとおり、このネストされたセットは永遠に続く可能性があります..

返したいのはこんな感じ

$categories("Root" => array("Root MySite.com" => array("Widgets" => array("Blue Widgets",...))))

[編集] : 再帰関数の開始点を貼り付けて、アリーまたはオブジェクトを単純に「平坦化」します。これを変更して、探しているデータ構造を取得できると思いますが、完全に正しく取得できませんでした。

    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 の方法があるのでしょうか?

4

3 に答える 3

5

これを試して

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

    return empty($return) ? null : $return;
}
于 2013-01-09T20:34:50.060 に答える
1

実用的な回答を書く時間はありませんが、それを行うための疑似コードをいくつか示します (半分は 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);
于 2013-01-09T20:26:38.487 に答える