2

そのように編成されたmysqlデータベースから配列を作成する必要があります

id    description    parentId    
1      Level 1        0           
2      Level 2        0           
3      Level 1a       1   
4      Level 1b       1 
5      Level 1a1      3
6      Level 1a1a     5

出力は次のようになります。

Level 1
      Level 1a
           Level 1a1
                Level 1a1a
      Level 1b
Level 2

ただし、私の現在のコードは第 2 レベルにのみ出力し、他のすべての子を独自の親にします。以下は現在のコードです。

$query = "SELECT * FROM pB_test ORDER BY parentId ASC";
$result = mysql_query($query) or die ('Database Error (' . mysql_errno() . ') ' . mysql_error());

$tree = array();

while($row = mysql_fetch_assoc($result)) 
{
    if($row['parentId'] == 0) 
    {
        $row['Children'] = array();
        $tree[$row['id']] = array(
                                'id' => $row['id'], 
                                'description' => $row['description'], 
                                'parentId' => $row['parentId']
                            );
    } 
    else 
    {
        $tree[$row['parentId']]['Children'][$row['id']] = $row['description'];
    }
}

$count = array_keys($tree);

foreach ($count as $array)
{
    ksort($tree[$array]['Children']);
}

echo print_r($tree, true);

正しい方向への助けやナッジは素晴らしいでしょう. 乾杯

更新:作業コード

    $results = array();
while($row=mysql_fetch_assoc($dbresult)) 
{ 
    $results[]=$row;

    $tree = null;
foreach($results as $result)
{
    $thisref = &$refs->{$result['id']};
    foreach($result as $k => $v)
    {
        $thisref->{$k} = $v;
    }
    if ($result['parentId'] == 0) {
        $tree->{$result['id']} = &$thisref;
    } else {
        $refs->{$result['parentId']}->children->{$result['id']} = &$thisref;
    }
}

$tree; // contains the newly sorted tree.

}

print_r($tree);
4

2 に答える 2

4

親子配列をグループ化するためのこのコードは驚くべきものであることがわかりました。私は4つの深さでテストしましたが、これまでに問題はありませんでした. ただし、再帰関数ではありません。

$tree = null;
foreach($results as $result)
{
    $thisref = &$refs->{$result['id']};
    foreach($result as $k => $v)
    {
        $thisref->{$k} = $v;
    }
    if ($result['parentId'] == 0) {
        $tree->{$result['id']} = &$thisref;
    } else {
        $refs->{$result['parentId']}->children->{$result['id']} = &$thisref;
    }
}

$tree; // contains the newly sorted tree.

状況に合わせて完全に機能させるには、いくつかの変更が必要になる場合があります。しかし、基本的にはすべての結果をループし、それらを参照によって結合します。

$tree終了データ型は であり、objectではないことに注意してくださいarray

幸運を

アップデート

配列をそのまま作成できます

$query = "SELECT * FROM pB_test ORDER BY parentId ASC";
$dbresult = mysql_query($query) or die ('Database Error (' . mysql_errno() . ') ' . mysql_error());

$results = array();
while($row=mysql_fetch_assoc($dbresult)) 
{ 
    $results[]=$row 
}
于 2012-06-13T03:41:40.280 に答える
1

ネストされたオブジェクトを簡単に検索できるように、オブジェクトを作成するときに id からオブジェクトにマップする配列を作成すると、おそらく最も簡単でしょう。基本的:

$tree = array();
$lookup = array();

while($row = mysql_fetch_assoc($result))  
{
    $object = array('id' => $row['id'],        
                    'description' => $row['description'],        
                    'parentId' => $row['parentId']);

    $lookup[$row['id']] = $object;

    $parentId = $row['parentId'];
    if ($parentId == 0)
    {
        $tree[] = $object;
    }
    else
    {
        $lookup[$parentId]['Children'][] = $object;
    }
}
于 2012-06-13T03:49:51.410 に答える