2015-12-03 65 views
1

排序陣列我有一個返回SQL查詢:由家長/遞歸

Array 
(
    [0] => stdClass Object 
     (
      [id] => 1 
      [parent] => 
      [created_at] => 2015 
      [updated_at] => 0 
      [name] => Strona Główna 
      [short] => 
      [content] => 
      [header] => 
      [img] => 
      [visible] => 0 
      [position] => 1000 
      [top] => 0 
      [left] => 0 
      [footer] => 0 
      [dropdown] => 0 
      [chilldren] => 0 
      [title] => 
      [description] => 
      [keywords] => 
      [tags] => 
      [redirect] => 
      [js] => 
      [css] => 
      [css_class] => 
      [module] => 
      [lang] => 0 
     ) 

    [1] => stdClass Object 
     (
      [id] => 2 
      [parent] => 1 
      [created_at] => 2015 
      [updated_at] => 0 
      [name] => Podstrona strony głównej 
      [short] => 
      [content] => 
      [header] => 
      [img] => 
      [visible] => 0 
      [position] => 1000 
      [top] => 0 
      [left] => 0 
      [footer] => 0 
      [dropdown] => 0 
      [chilldren] => 0 
      [title] => 
      [description] => 
      [keywords] => 
      [tags] => 
      [redirect] => 
      [js] => 
      [css] => 
      [css_class] => 
      [module] => 
      [lang] => 0 
     ) 

    [2] => stdClass Object 
     (
      [id] => 3 
      [parent] => 
      [created_at] => 2015 
      [updated_at] => 2015 
      [name] => O nas 
      [short] => 
      [content] => 
      [header] => 
      [img] => 
      [visible] => 0 
      [position] => 1000 
      [top] => 0 
      [left] => 0 
      [footer] => 0 
      [dropdown] => 0 
      [chilldren] => 0 
      [title] => 
      [description] => 
      [keywords] => 
      [tags] => 
      [redirect] => 
      [js] => 
      [css] => 
      [css_class] => 
      [module] => 
      [lang] => 0 
     ) 

    [3] => stdClass Object 
     (
      [id] => 5 
      [parent] => 1 
      [created_at] => 2015 
      [updated_at] => 2015 
      [name] => Kolejna podstrona 
      [short] => 
      [content] => 
      [header] => 
      [img] => 
      [visible] => 0 
      [position] => 1000 
      [top] => 0 
      [left] => 0 
      [footer] => 0 
      [dropdown] => 0 
      [chilldren] => 0 
      [title] => 
      [description] => 
      [keywords] => 
      [tags] => 
      [redirect] => 
      [js] => 
      [css] => 
      [css_class] => 
      [module] => 
      [lang] => 0 
     ) 

    [4] => stdClass Object 
     (
      [id] => 6 
      [parent] => 3 
      [created_at] => 2015 
      [updated_at] => 0 
      [name] => Kolejna podstrona 
      [short] => 
      [content] => 
      [header] => 
      [img] => 
      [visible] => 0 
      [position] => 1000 
      [top] => 0 
      [left] => 0 
      [footer] => 0 
      [dropdown] => 0 
      [chilldren] => 0 
      [title] => 
      [description] => 
      [keywords] => 
      [tags] => 
      [redirect] => 
      [js] => 
      [css] => 
      [css_class] => 
      [module] => 
      [lang] => 0 
     ) 

    [5] => stdClass Object 
     (
      [id] => 8 
      [parent] => 5 
      [created_at] => 2015 
      [updated_at] => 0 
      [name] => tuytuytuyt 
      [short] => 
      [content] => 
      [header] => 
      [img] => 
      [visible] => 0 
      [position] => 1000 
      [top] => 0 
      [left] => 0 
      [footer] => 0 
      [dropdown] => 0 
      [chilldren] => 0 
      [title] => 
      [description] => 
      [keywords] => 
      [tags] => 
      [redirect] => 
      [js] => 
      [css] => 
      [css_class] => 
      [module] => 
      [lang] => 0 
     ) 

    [6] => stdClass Object 
     (
      [id] => 9 
      [parent] => 5 
      [created_at] => 2015 
      [updated_at] => 0 
      [name] => fghfhgfh 
      [short] => 
      [content] => 
      [header] => 
      [img] => 
      [visible] => 0 
      [position] => 1000 
      [top] => 0 
      [left] => 0 
      [footer] => 0 
      [dropdown] => 0 
      [chilldren] => 0 
      [title] => 
      [description] => 
      [keywords] => 
      [tags] => 
      [redirect] => 
      [js] => 
      [css] => 
      [css_class] => 
      [module] => 
      [lang] => 0 
     ) 

) 

而且我幾乎成功到達排序。如果元素有一個父元素,它會創建一個數組並將子元素添加到此元素,但是最後一個元素存在問題,我不知道爲什麼。

現在看起來就像這樣:

Array 
(
    [0] => stdClass Object 
     (
      [id] => 1 
      [parent] => 
      [created_at] => 2015 
      [updated_at] => 0 
      [name] => Strona Główna 
      [short] => 
      [content] => 
      [header] => 
      [img] => 
      [visible] => 0 
      [position] => 1000 
      [top] => 0 
      [left] => 0 
      [footer] => 0 
      [dropdown] => 0 
      [chilldren] => 0 
      [title] => 
      [description] => 
      [keywords] => 
      [tags] => 
      [redirect] => 
      [js] => 
      [css] => 
      [css_class] => 
      [module] => 
      [lang] => 0 
      [node] => Array 
       (
        [0] => stdClass Object 
         (
          [id] => 2 
          [parent] => 1 
          [created_at] => 2015 
          [updated_at] => 0 
          [name] => Podstrona strony głównej 
          [short] => 
          [content] => 
          [header] => 
          [img] => 
          [visible] => 0 
          [position] => 1000 
          [top] => 0 
          [left] => 0 
          [footer] => 0 
          [dropdown] => 0 
          [chilldren] => 0 
          [title] => 
          [description] => 
          [keywords] => 
          [tags] => 
          [redirect] => 
          [js] => 
          [css] => 
          [css_class] => 
          [module] => 
          [lang] => 0 
          [node] => Array 
           (
           ) 

         ) 

        [1] => stdClass Object 
         (
          [id] => 5 
          [parent] => 1 
          [created_at] => 2015 
          [updated_at] => 2015 
          [name] => Kolejna podstrona 
          [short] => 
          [content] => 
          [header] => 
          [img] => 
          [visible] => 0 
          [position] => 1000 
          [top] => 0 
          [left] => 0 
          [footer] => 0 
          [dropdown] => 0 
          [chilldren] => 0 
          [title] => 
          [description] => 
          [keywords] => 
          [tags] => 
          [redirect] => 
          [js] => 
          [css] => 
          [css_class] => 
          [module] => 
          [lang] => 0 
          [node] => Array 
           (
            [0] => stdClass Object 
             (
              [id] => 8 
              [parent] => 5 
              [created_at] => 2015 
              [updated_at] => 0 
              [name] => tuytuytuyt 
              [short] => 
              [content] => 
              [header] => 
              [img] => 
              [visible] => 0 
              [position] => 1000 
              [top] => 0 
              [left] => 0 
              [footer] => 0 
              [dropdown] => 0 
              [chilldren] => 0 
              [title] => 
              [description] => 
              [keywords] => 
              [tags] => 
              [redirect] => 
              [js] => 
              [css] => 
              [css_class] => 
              [module] => 
              [lang] => 0 
              [node] => Array 
               (
               ) 

             ) 

            [1] => stdClass Object 
             (
              [id] => 9 
              [parent] => 5 
              [created_at] => 2015 
              [updated_at] => 0 
              [name] => fghfhgfh 
              [short] => 
              [content] => 
              [header] => 
              [img] => 
              [visible] => 0 
              [position] => 1000 
              [top] => 0 
              [left] => 0 
              [footer] => 0 
              [dropdown] => 0 
              [chilldren] => 0 
              [title] => 
              [description] => 
              [keywords] => 
              [tags] => 
              [redirect] => 
              [js] => 
              [css] => 
              [css_class] => 
              [module] => 
              [lang] => 0 
              [node] => Array 
               (
               ) 

             ) 

           ) 

         ) 

       ) 

     ) 

    [1] => stdClass Object 
     (
      [id] => 2 
      [parent] => 1 
      [created_at] => 2015 
      [updated_at] => 0 
      [name] => Podstrona strony głównej 
      [short] => 
      [content] => 
      [header] => 
      [img] => 
      [visible] => 0 
      [position] => 1000 
      [top] => 0 
      [left] => 0 
      [footer] => 0 
      [dropdown] => 0 
      [chilldren] => 0 
      [title] => 
      [description] => 
      [keywords] => 
      [tags] => 
      [redirect] => 
      [js] => 
      [css] => 
      [css_class] => 
      [module] => 
      [lang] => 0 
      [node] => Array 
       (
       ) 

     ) 

    [2] => stdClass Object 
     (
      [id] => 3 
      [parent] => 
      [created_at] => 2015 
      [updated_at] => 2015 
      [name] => O nas 
      [short] => 
      [content] => 
      [header] => 
      [img] => 
      [visible] => 0 
      [position] => 1000 
      [top] => 0 
      [left] => 0 
      [footer] => 0 
      [dropdown] => 0 
      [chilldren] => 0 
      [title] => 
      [description] => 
      [keywords] => 
      [tags] => 
      [redirect] => 
      [js] => 
      [css] => 
      [css_class] => 
      [module] => 
      [lang] => 0 
      [node] => Array 
       (
        [0] => stdClass Object 
         (
          [id] => 6 
          [parent] => 3 
          [created_at] => 2015 
          [updated_at] => 0 
          [name] => Kolejna podstrona 
          [short] => 
          [content] => 
          [header] => 
          [img] => 
          [visible] => 0 
          [position] => 1000 
          [top] => 0 
          [left] => 0 
          [footer] => 0 
          [dropdown] => 0 
          [chilldren] => 0 
          [title] => 
          [description] => 
          [keywords] => 
          [tags] => 
          [redirect] => 
          [js] => 
          [css] => 
          [css_class] => 
          [module] => 
          [lang] => 0 
          [node] => Array 
           (
           ) 

         ) 

       ) 

     ) 

    [3] => stdClass Object 
     (
      [id] => 5 
      [parent] => 1 
      [created_at] => 2015 
      [updated_at] => 2015 
      [name] => Kolejna podstrona 
      [short] => 
      [content] => 
      [header] => 
      [img] => 
      [visible] => 0 
      [position] => 1000 
      [top] => 0 
      [left] => 0 
      [footer] => 0 
      [dropdown] => 0 
      [chilldren] => 0 
      [title] => 
      [description] => 
      [keywords] => 
      [tags] => 
      [redirect] => 
      [js] => 
      [css] => 
      [css_class] => 
      [module] => 
      [lang] => 0 
      [node] => Array 
       (
        [0] => stdClass Object 
         (
          [id] => 8 
          [parent] => 5 
          [created_at] => 2015 
          [updated_at] => 0 
          [name] => tuytuytuyt 
          [short] => 
          [content] => 
          [header] => 
          [img] => 
          [visible] => 0 
          [position] => 1000 
          [top] => 0 
          [left] => 0 
          [footer] => 0 
          [dropdown] => 0 
          [chilldren] => 0 
          [title] => 
          [description] => 
          [keywords] => 
          [tags] => 
          [redirect] => 
          [js] => 
          [css] => 
          [css_class] => 
          [module] => 
          [lang] => 0 
          [node] => Array 
           (
           ) 

         ) 

        [1] => stdClass Object 
         (
          [id] => 9 
          [parent] => 5 
          [created_at] => 2015 
          [updated_at] => 0 
          [name] => fghfhgfh 
          [short] => 
          [content] => 
          [header] => 
          [img] => 
          [visible] => 0 
          [position] => 1000 
          [top] => 0 
          [left] => 0 
          [footer] => 0 
          [dropdown] => 0 
          [chilldren] => 0 
          [title] => 
          [description] => 
          [keywords] => 
          [tags] => 
          [redirect] => 
          [js] => 
          [css] => 
          [css_class] => 
          [module] => 
          [lang] => 0 
          [node] => Array 
           (
           ) 

         ) 

       ) 

     ) 

    [4] => stdClass Object 
     (
      [id] => 6 
      [parent] => 3 
      [created_at] => 2015 
      [updated_at] => 0 
      [name] => Kolejna podstrona 
      [short] => 
      [content] => 
      [header] => 
      [img] => 
      [visible] => 0 
      [position] => 1000 
      [top] => 0 
      [left] => 0 
      [footer] => 0 
      [dropdown] => 0 
      [chilldren] => 0 
      [title] => 
      [description] => 
      [keywords] => 
      [tags] => 
      [redirect] => 
      [js] => 
      [css] => 
      [css_class] => 
      [module] => 
      [lang] => 0 
      [node] => Array 
       (
       ) 

     ) 

    [5] => stdClass Object 
     (
      [id] => 8 
      [parent] => 5 
      [created_at] => 2015 
      [updated_at] => 0 
      [name] => tuytuytuyt 
      [short] => 
      [content] => 
      [header] => 
      [img] => 
      [visible] => 0 
      [position] => 1000 
      [top] => 0 
      [left] => 0 
      [footer] => 0 
      [dropdown] => 0 
      [chilldren] => 0 
      [title] => 
      [description] => 
      [keywords] => 
      [tags] => 
      [redirect] => 
      [js] => 
      [css] => 
      [css_class] => 
      [module] => 
      [lang] => 0 
      [node] => Array 
       (
       ) 

     ) 

    [6] => stdClass Object 
     (
      [id] => 9 
      [parent] => 5 
      [created_at] => 2015 
      [updated_at] => 0 
      [name] => fghfhgfh 
      [short] => 
      [content] => 
      [header] => 
      [img] => 
      [visible] => 0 
      [position] => 1000 
      [top] => 0 
      [left] => 0 
      [footer] => 0 
      [dropdown] => 0 
      [chilldren] => 0 
      [title] => 
      [description] => 
      [keywords] => 
      [tags] => 
      [redirect] => 
      [js] => 
      [css] => 
      [css_class] => 
      [module] => 
      [lang] => 0 
      [node] => Array 
       (
       ) 

     ) 

) 

正如你所看到的最後的元素進行過濾。

守則「排序」父母的模樣:

  $new = []; 
      foreach($data as $key => $item) { 
       $data[$key] - > node = []; 
       array_push($new, $data[$key]); 

       for ($i = 0; $i < count($new); $i++) { 
       if ($new[$i] - > id == $data[$key] - > parent) { 
        array_push($new[$i] - > node, $data[$key]); 
       } 

       } 
      } 

      print_r($data); 

誰能給我解釋一下我做錯了?

+0

輸出中最後一個元素有什麼問題? ID爲6,8和9的節點沒有子節點,所以它們單獨出現,我會發現它是正常的。你可以向你的問題添加你想要的輸出嗎? – trincot

+0

他們屬於特定的「父母」,因爲你可以看到我不想和我不工作與「兒童」的關鍵。 – Mehow

回答

0

您可以使用一個遞歸函數,就像這樣:

function makeTree($data, $parent) { 
    $tree = []; 
    foreach($data as $item) { 
     if ($item -> parent == $parent) { 
      $item -> node = makeTree($data, $item -> id); 
      $tree[] = $item; 
     } 
    } 
    return $tree; 
} 

$new = makeTree($data, null); 

print_r($new); 

此功能使定父項的所有後代的樹。當元素沒有子元素時,遞歸停止,然後該函數返回一個空數組。

調用者使用函數(數組)的返回值,將其添加到父節點的node屬性中。

該函數首先由根值調用,即當沒有父項時分配給parent屬性的值。在你的情況是這是null,所以這就是爲什麼主要電話有null作爲第二個參數。

0

當您遍歷結果時,您應該詢問當前元素是否具有父級。如果元素沒有父,那麼只需將元素添加到新陣列,但如果該元素具有父然後將其添加到該元素的節點列表:

$new = []; 
foreach($data as $key => $item) {  
    if(!empty($item->parent)) 
    $new[$item->id]->node[]= $item; 
    else 
    $new[$item->id]= $item; 
} 
print_r($data); 

當然會,如果只工作元素的父元素位於結果集中的元素之前。

+0

您的代碼無法解決我的問題。它產生了與我在我的問題開始時引用的結果相同的結果,但唯一的區別是它將父元素與元素一起發送,並將它發送到新創建的「節點」之下。 – Mehow