2012-08-31 9 views
10

すべての子をそれぞれの親の後に並べ替えるにはどうすればよいですか?私は1次元配列の内部に木を格納しようとしていると思います。私はusortを使ってこれを理解しようとしましたが、それがその仕事のための適切なツールではないと思います。親に応じて配列をソートする。 1次元配列のツリー

例入力配列:

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のキー配列を配列の最後の要素に移動する必要があります –

関連する問題