phparraysmultidimensional-arrayhierarchical-clustering

Display hierarchical Multidimensional Array with parent child numbering


I have a multidimensional array which has parent child relationship.

I want to display this as a list of numerical value representing their parents in an appropriate order.

Array
(
    [0] => Array
        (
            [name] => Category 1
            [id] => 6
            [children] => Array
                (
                    [0] => Array
                        (
                            [name] => Category 2
                            [id] => 7
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => cricket1
                                            [id] => 19
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [name] => cricket2
                                                            [id] => 21
                                                            [children] => 
                                                        )

                                                )

                                        )

                                )

                        )

                    [1] => Array
                        (
                            [name] => another category
                            [id] => 15
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => new 1
                                            [id] => 18
                                            [children] => 
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [name] => Category 3
            [id] => 8
            [children] => Array
                (
                    [0] => Array
                        (
                            [name] => new category
                            [id] => 11
                            [children] => 
                        )

                )

        )

    [2] => Array
        (
            [name] => Sazzad
            [id] => 9
            [children] => Array
                (
                    [0] => Array
                        (
                            [name] => Mahmud
                            [id] => 10
                            [children] => 
                        )

                )

        )

)

I want to show this array as given below :

    1.      Category 1
    1.1     Category 2
    1.1.1   cricket1
    1.1.1.1 cricket2
    1.2     another category
    1.2.1   new 1
    2.      Category 3
    2.1     new category
    3.      Sazzad
    3.1     Mahmud

How Can I achieve this numbering system using PHP??

Thanks in advance.


Solution

  • You could use simple recursive function:

    function makeOneLevelArray($in, &$out, $prefixLevel = '') {
        $level = 1;
        foreach ($in as $v) {
            $out[] = $prefixLevel . $level . ' ' . $v['name'];
            if (!empty($v['children'])) {
                makeOneLevelArray($v['children'], $out, $prefixLevel . $level . '.');
            }
            $level++;
        }
    }
    
    $outputArray = array();
    makeOneLevelArray($inputArray, $outputArray);
    print_r($outputArray);
    

    demo