Konwertuj tablicę płaską na wielowymiarową

Mam tablicę z danymi drzewa (według ID rodzica). Chcę ją przekonwertować na wielowymiarową tablicę. Jaki jest najlepszy sposób, aby to osiągnąć? Czy jest na to jakaś krótka funkcja?

Tablica źródłowa:

$source = array(
    '0' => array(
            'Menu' => array(
                    'id' => 45
                    'name' => 'Home'
                    'parent_id' => 1
            )
    )
    '1' => array(
            'Menu' => array(
                    'id' => 47
                    'name' => 'Get started'
                    'parent_id' => 1
            )
    )
    '2' => array(
            'Menu' => array(
                    'id' => 72
                    'name' => 'Attributes'
                    'parent_id' => 71
            )
    )
    '3' => array(
            'Menu' => array(
                    'id' => 73
                    'name' => 'Headings'
                    'parent_id' => 71
            )
    )
    '4' => array(
            'Menu' => array(
                    'id' => 75
                    'name' => 'Links'
                    'parent_id' => 71
            )
    )
    '5' => array(
            'Menu' => array(
                    'id' => 59
                    'name' => 'Images'
                    'parent_id' => 75
            )
    )
    '6' => array(
            'Menu' => array(
                    'id' => 65
                    'name' => 'Lists'
                    'parent_id' => 75
            )
    )
);

Niektórych rodziców brakuje w tablicy źródłowej. Chciałbym, aby elementy z brakującym rodzicem były root. Tablica wyników:

$result = array(
    '0' => array(
            'Menu' => array(
                    'id' => 45
                    'name' => 'Home'
                    'parent_id' => 1
            )
            'Children' => array()
    )
    '1' => array(
            'Menu' => array(
                    'id' => 47
                    'name' => 'Get started'
                    'parent_id' => 1
            )
            'Children' => array()
    )
    '2' => array(
            'Menu' => array(
                    'id' => 72
                    'name' => 'Attributes'
                    'parent_id' => 71
            )
            'Children' => array()
    )
    '3' => array(
            'Menu' => array(
                    'id' => 73
                    'name' => 'Headings'
                    'parent_id' => 71
            )
            'Children' => array()
    )
    '4' => array(
            'Menu' => array(
                    'id' => 75
                    'name' => 'Links'
                    'parent_id' => 71
            )
            'Children' => array(
                    '0' => array(
                        'Menu' => array(
                            'id' => 59
                            'name' => 'Images'
                            'parent_id' => 75
                        )
                        'Children' => array()
                    )
                    '1' => array(
                        'Menu' => array(
                            'id' => 65
                            'name' => 'Lists'
                            'parent_id' => 75
                        )
                        'Children' => array()
                   )
            )
     )
);

Aktualizacja: usunięto nawiasy kwadratowe.

Author: Cœur, 2010-07-16

3 answers

Myślę, że nie ma wbudowanej funkcji w PHP, która to robi.

Próbowałem poniższego kodu i wydaje się, że działa, aby przygotować zagnieżdżoną tablicę tak, jak ją opisujesz:

$nodes = array();
$tree = array();
foreach ($source as &$node) {
  $node["Children"] = array();
  $id = $node["Menu"]["id"];
  $parent_id = $node["Menu"]["parent_id"];
  $nodes[$id] =& $node;
  if (array_key_exists($parent_id, $nodes)) {
    $nodes[$parent_id]["Children"][] =& $node;
  } else {
    $tree[] =& $node;
  }
}

var_dump($tree);

Napisałem podobny algorytm w klasie PHP, którą napisałem do mojej prezentacji hierarchiczne modele w SQL i PHP, ale używałem obiektów zamiast zwykłych tablic.

 16
Author: Bill Karwin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-07-16 01:43:19

Napisałem ten wariant biorąc pod uwagę, że root parent_id jest 0 lub brak. Bez względu na dzieci po rodzicach w DB ($source) lub nie.

$source_by_id = array();
foreach ($source as &$row){
  $source_by_id[$row['id']] = &$row;
}
foreach ($source_by_id as $id => &$row){
  $source_by_id[ intval($row['parent_id']) ]['children'][$id] = &$row;
}
// remove cycling itself
unset($source_by_id[0]['children'][0]);

$result = $source_by_id[0]['children'];

Klucze tablicy wyników są odpowiednimi identyfikatorami. Smacznego!

 0
Author: vitalymak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-29 09:43:08

Szukałem przykładu, jak to zrobić, z kategoriami. Ten przykład zakłada, że rodzice zawsze będą mieli identyfikator rodzica równy '0'. Przykładem jest użycie ZF2.

Brak odniesień, ani rekurencji. Sztuczka znajduje się na wyjściu, poszukujesz indeksu [0], a Dla Dzieci określasz parent_id jako indeks.

$categoryLookup = $this->getCategoryLookup($associateById=true);

if ($assignedCategories) {          
    $categoryHeirarchy = array();
    foreach($assignedCategories as $assignedCategory) {
        $child = $categoryLookup[$assignedCategory->category_id];
        $parent = $categoryLookup[$child->parent_id];               
        $categoryHeirarchy[$child->parent_id][] = $categoryLookup[$child->category_id];
        $categoryHeirarchy[$parent->parent_id][$parent->category_id] = $categoryLookup[$parent->category_id];
    }           

    return $categoryHeirarchy;  
}


<h3>Categories</h3>
<dl class="dl-horizontal">
    <?php foreach($this->categoryHeirarchy[0] as $parent): ?>
        <dt><?php echo $this->escapeHtml($parent->name); ?></dt>
        <?php foreach($this->categoryHeirarchy[$parent->category_id] as $child): ?>
            <dd><?php echo $this->escapeHtml($child->name); ?></dd>
        <?php endforeach; ?>
    <?php endforeach; ?>                    
</dl>
 0
Author: Eduardo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-05 18:47:34