2013-07-03 45 views
-4

我有一個存儲ID陣列>父如何建立一個功能uasort將兒童在父母

我想要做的是排序這個數組使用uasort函數在PHP將所有子女父母下

所以,如果我有

ID>>PARENT 
1 >> 0 
2 >> 0 
3 >> 1 
4 >> 1 
5 >> 0 
6 >> 2 
7 >> 6 
8 >> 6 
9 >> 2 

然後,我需要回到這樣

ID 
1 
3 
4 
2 
6 
7 
8 
9 
結果

我的陣列是這樣

Array (
    [0] => Array 
     (
      [ID] => 1 
      [PARENT] => 0 
     ) 

    [1] => Array 
     (
      [ID] => 2 
      [PARENT] => 0 
     ) 

    [2] => Array 
     (
      [ID] => 3 
      [PARENT] => 1 
     ) 

    [3] => Array 
     (
      [ID] => 4 
      [PARENT] => 1 
     ) 

    [4] => Array 
     (
      [ID] => 5 
      [PARENT] => 0 
     ) 
    [5] => Array 
     (
      [ID] => 6 
      [PARENT] => 2 
     ) 

    [6] => Array 
     (
      [ID] =>7 
      [PARENT] => 6 
     ) 

    [7] => Array 
     (
      [ID] =>8 
      [PARENT] => 6 
     ) 

    [8] => Array 
     (
      [ID] =>9 
      [PARENT] => 9 
     ) 
) 

這是後的孩子家長,而不是在它們下面我做了什麼

uasort($survey, 'cmp'); 

print_r($survey); 


function cmp($a, $b) { 
    if ($a['parent_id'] == $b['parent_id']) { 
     return 0; 
    } 
    return ($a['parent_id'] < $b['parent_id']) ? -1 : 1; 
} 

這個代碼排序所有的父母先。

有人可以幫我解決這個問題嗎?

+0

如果您繼續複製您的帖子,您很快就會發現自己無法再發布帖子...如果您還沒有發佈帖子, –

回答

3

uasort()和朋友不太適合這裏的purpouse:假設你想比較父母N的最後一個孩子和下一個父母N + 1 - 孩子應該比較小一點。如果同一個孩子有父母N + 1,則需要比較大一些。這是可行的,只要你只有一個級別,但如果你有更多的級別,它會變得很難受。

我建議使用另一種方法:

  • 步驟1:由父組條目到數組的數組
  • 步驟2:排序子陣列單獨地
  • 步驟3:拼合結構通過遞歸:
    • 開始與當前父ID爲0和一個空的最後
    • 重複
      • 舉動與目前的父ID的陣列的第一行到最後陣列
      • 的端部
      • 如果沒有可用的,與此行的id返回
      • 遞歸作爲當前父ID

編輯

一些代碼,如要求:這適用於我的例子。

請注意,您的示例看起來有點彈性:您希望輸出缺少ID 5,而大多數根節點使用0作爲父項,ID 9使用ID == PARENT。

<?php 
function flatten($parentid, &$parents, &$final) { 
    if (!isset($parents[$parentid])) return; 
    $children=$parents[$parentid]; 
    unset($parents[$parentid]); 

    //repeat 
    while (true) { 
    //move the first row of the array with the current parent id to the end of the final array 
    $child=array_shift($children); 

    //if none available, return 
    if (!$child) break; 
    $final[]=$child; 

    //recurse with the id of this row as the current parent id 
    flatten($child['ID'],$parents,$final); 
    } 
} 

//Step 1: Group entries into an array of arrays by parent 
//In your input, both PARENT==ID and PARENT==0 are used for root nodes 
$parents=array(); 
foreach ($input as $item) { 
    $parent=$item['PARENT']; 
    if ($parent==$item['ID']) $parent=0; 
    if (isset($parents[$parent])) $parents[$parent][$item['ID']]=$item; 
    else $parents[$parent]=array($item['ID']=>$item); 
} 

//Step 2: Sort the sub-arrays individually 
foreach ($parents as $item) ksort($item); 

//Step 3: Flatten the structure by recursion: 
//start with current parent id 0 and an empty final 
$final=array(); 
flatten(0, $parents, $final); 

//Done 
print_r($final); 
?> 
+0

你能幫我一些代碼嗎?我正在嘗試編寫這段代碼,但我沒有想出預期的結果。 – Mike

0

循環數組並構建排序版本。

$arr2 = array(); 
    foreach ($arr as $k => $v) { 
     if ($v['PARENT'] > 0) { 
      $arr2[$v['PARENT']][$k] = $v; 
     } 
    } 
ksort($arr2); 

使用您的示例(並更正您的信息中的不匹配),我得到以下內容。

Array 
(
    [1] => Array 
     (
      [3] => Array 
       (
        [ID] => 3 
        [PARENT] => 1 
       ) 
      [4] => Array 
       (
        [ID] => 4 
        [PARENT] => 1 
       ) 
     ) 
    [2] => Array 
     (
      [6] => Array 
       (
        [ID] => 6 
        [PARENT] => 2 
       ) 
      [9] => Array 
       (
        [ID] => 9 
        [PARENT] => 2 
       ) 
     ) 
    [6] => Array 
     (
      [7] => Array 
       (
        [ID] => 7 
        [PARENT] => 6 
       ) 

      [8] => Array 
       (
        [ID] => 8 
        [PARENT] => 6 
       ) 
     ) 
) 
+0

這不起作用。你需要在解決方案中包含ID嗎? – Mike