2012-08-31 157 views
10

如何將數組與所有孩子分別放在各自的父母后面?我想我試圖在一維數組中存儲一棵樹。我試圖用usort來解決這個問題,但我認爲它不適合這項工作。根據父級對數組進行排序;一維數組中的一棵樹

示例輸入數組:

array (0 => array ('id' => '1', 'parent' => '0',), 
1 => array ('id' => '2', 'parent' => '1',), 
2 => array ('id' => '3', 'parent' => '0',), 
3 => array ('id' => '5', 'parent' => '0',), 
4 => array ('id' => '17', 'parent' => '3',), 
5 => array ('id' => '31', 'parent' => '2',), 
6 => array ('id' => '32', 'parent' => '2',)) 

輸出示例:

Array sorted according to the description

+0

多少級將這個數組有? –

+2

我不理解你的問題。我們究竟在這裏想達到什麼目的?匹配父母號碼的ID? – Mic1780

回答

6

開始通過建立一個實際的樹,然後弄平該樹:

$array = array (0 => array ('id' => '1', 'parent' => '0',), 
       1 => array ('id' => '2', 'parent' => '1',), 
       2 => array ('id' => '3', 'parent' => '0',), 
       3 => array ('id' => '5', 'parent' => '0',), 
       4 => array ('id' => '17', 'parent' => '3',), 
       5 => array ('id' => '31', 'parent' => '2',), 
       6 => array ('id' => '32', 'parent' => '2',)); 

/* Building a tree. We also save a map of references to avoid         
    searching the tree for nodes */ 

//Helper to create nodes                  
$tree_node = function($id, $parent) { 
    return array('id' => $id, 'parent' => $parent, 'children' => array()); 
}; 

$tree = $tree_node(0, null); //root node              
$map = array(0 => &$tree); 
foreach($array as $cur) { 
    $id = (int) $cur['id']; 
    $parentId = (int) $cur['parent']; 
    $map[$id] =& $map[$parentId]['children'][]; 
    $map[$id] = $tree_node($id, $parentId); 
} 

//Now recursively flatten the tree:               
function flatter($node) { 
    //Create an array element of the node            
    $array_element = array('id' => (string) $node['id'], 
         'parent' => (string) $node['parent']); 
    //Add all children after me                 
    $result = array($array_element); 
    foreach($node['children'] as $child) { 
    $result = array_merge($result, flatter($child)); 
    } 
    return $result; 
} 

$array = flatter($tree); 
array_shift($array); //Remove the root node, which was only added as a helper     

print_r($array); 
+0

你好,EmilVikström。好吧!謝謝! –

-1
<?php 

/** 
* @author Prasath A.R 
* @copyright 2012 
* @Date 2012-8-31 17:14 
*/ 

$array = array (0 => array ('id' => '1', 'parent' => '0',), 
       1 => array ('id' => '2', 'parent' => '1',), 
       2 => array ('id' => '3', 'parent' => '0',), 
       3 => array ('id' => '5', 'parent' => '0',), 
       4 => array ('id' => '17', 'parent' => '3',), 
       5 => array ('id' => '31', 'parent' => '2',), 
       6 => array ('id' => '32', 'parent' => '2',)); 

print_r($array); 
echo "<br />"; 

for($i=0;$i<count($array);$i++) 
{ 
for($j=$i;$j<count($array);$j++) 
{ 
     if($array[$i]['parent']>$array[$j]['parent']) 
     { 
      $temp=$array[$i]; 
      $array[$i]=$array[$j]; 
      $array[$j]=$temp; 
     } 
    } 
} 

echo "<h2>After Sorting</h2><br />"; 
print_r($array); 

?> 

答案將是:

陣列 ( [0] =>數組 ( [ID] => 1 [父] => 0 )

[1] => Array 
    (
     [id] => 2 
     [parent] => 1 
    ) 

[2] => Array 
    (
     [id] => 3 
     [parent] => 0 
    ) 

[3] => Array 
    (
     [id] => 5 
     [parent] => 0 
    ) 

[4] => Array 
    (
     [id] => 17 
     [parent] => 3 
    ) 

[5] => Array 
    (
     [id] => 31 
     [parent] => 2 
    ) 

[6] => Array 
    (
     [id] => 32 
     [parent] => 2 
    ) 

分類後

陣列 ( [0] =>陣列 ( [ID] => 1 [父] => 0 )

[1] => Array 
    (
     [id] => 3 
     [parent] => 0 
    ) 

[2] => Array 
    (
     [id] => 5 
     [parent] => 0 
    ) 

[3] => Array 
    (
     [id] => 2 
     [parent] => 1 
    ) 

[4] => Array 
    (
     [id] => 31 
     [parent] => 2 
    ) 

[5] => Array 
    (
     [id] => 32 
     [parent] => 2 
    ) 

[6] => Array 
    (
     [id] => 17 
     [parent] => 3 
    ) 

+0

這與問題不符。你的結果數組不等於問題中的結果數組。 –

+0

@EmilVikström:實際上匹配的結果,我們需要將0鍵陣列移到數組的最後一個元素 –

相關問題