0

Here is my array output

Array
(
    [1] => Array
        (
            [id] => 1
            [name] => Category A
            [parent] => 0
        )
    [2] => Array
        (
            [id] => 2
            [name] => Sub Cat A - 1
            [parent] => 1
        )
    [3] => Array
        (
            [id] => 3
            [name] => Sub Cat A - 2
            [parent] => 1
        )
    [4] => Array
        (
            [id] => 4
            [name] => Sub Cat A 2 - 1
            [parent] => 3
        )
    [5] => Array
        (
            [id] => 5
            [name] => Category B
            [parent] => 0
        )
    [6] => Array
        (
            [id] => 6
            [name] => Sub Cat B - 1
            [parent] => 5
        )
    [7] => Array
        (
            [id] => 7
            [name] => Category C
            [parent] => 0
        )
)

I have try with this but wrong output.

foreach ($categories as $key => $category) {
    $group[$category['parent']][$key] = $category;
}

Question : How to regroup the array & my desire output should be like

Category A
    Sub Cat A - 1
    Sub Cat A - 2
        Sub Cat A 2 - 1
Category B
    Sub Cat B - 1
Category C
user1286499
  • 227
  • 2
  • 13

1 Answers1

2

try this code;

<?php
$as // your array
$new_as = array();
foreach ($as as $ind => $a) {
    $new_as[$a['id']] = $a;
    $new_as[$a['id']]['children'] = array();
}

foreach ($new_as as $ind => &$new_a) {
    $parent = $new_as[$ind]['parent'];
    if(isset($new_as[$parent])){
        $new_as[$parent]['children'][] = & $new_as[$ind];
    }
}
echo '<pre>';
foreach ($new_as as $ind => &$new_a) {
    if ($new_a['parent'] == 0) {
        echo $new_a['name'];
        echo "\n";
        print_children($new_a['children'],1);
    }
}
echo '</pre>';    
function print_children($children, $level = 0)
{
    foreach ($children as $child) {
        echo str_repeat("    ", $level);
        echo $child['name'];
        echo "\n";
        print_children($child['children'], $level + 1);
    }
}
seyed
  • 1,555
  • 1
  • 17
  • 24
  • 2
    This also ads all sub categories to the top level array in addition to making them threaded. Since printing the structure depends on whether the parent is '0' it does not show up. You can see this by var_dump'ing $new_as – display name Jan 05 '13 at 21:11
  • 1
    "Try this" answers are low-value on Stackoverflow because they do a poor job of educating/empowering the OP and thousands of future researchers. – mickmackusa Mar 17 '20 at 13:53
  • 1
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Dharman Mar 17 '20 at 13:58