Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert array structure for ul() codeigniter helper

I want to ask how to convert an array like below:

$arr = array(
    array(
        'id' => 1,
        'name' => 'Home',
        'link_to' => 'home.php',
        'parent' => 0,
        'level' => 1
    ),
    array(
        'id' => 2,
        'name' => 'About',
        'link_to' => 'about.php',
        'parent' => 0,
        'level' => 1
    ),
    array(
        'id' => 3,
        'name' => 'About Me',
        'link_to' => 'about-me.php',
        'parent' => 2,
        'level' => 2
    ),
    array(
        'id' => 4,
        'name' => 'About Us',
        'link_to' => 'about-us.php',
        'parent' => 2,
        'level' => 2
    ),
    array(
        'id' => 5,
        'name' => 'Contact Us',
        'link_to' => 'contact-us.php',
        'parent' => 4,
        'level' => 3
    ),
    array(
        'id' => 6,
        'name' => 'Blog',
        'link_to' => 'blog.php',
        'parent' => 0,
        'level' => 1
    ),
);

into this one:

$result = array(
    'Home',
    'About' => array(
        'About Me',
        'About Us' => array(
            'Contact Us'
        )
    ),
    'Blog'
);

there are element 'parent' id which can tell the parent array ( 0 = root) and there are also element 'level'.

I need that kind of array so I can create list using ul() function from codeigniter helper.

like image 1000
Permana Avatar asked Sep 27 '26 19:09

Permana


1 Answers

i had to do something similar to create a tree from data rows.

So, you have to work with references, it is easier than others ways.

The next code arrive to something similar of what you want (i think it's better structure if you makes changes later)

<?php

    $arr = array(
        array(
            'id' => 1,
            'name' => 'Home',
            'link_to' => 'home.php',
            'parent' => 0,
            'level' => 1
        ),
        array(
            'id' => 2,
            'name' => 'About',
            'link_to' => 'about.php',
            'parent' => 0,
            'level' => 1
        ),
        array(
            'id' => 3,
            'name' => 'About Me',
            'link_to' => 'about-me.php',
            'parent' => 2,
            'level' => 2
        ),
        array(
            'id' => 4,
            'name' => 'About Us',
            'link_to' => 'about-us.php',
            'parent' => 2,
            'level' => 2
        ),
        array(
            'id' => 5,
            'name' => 'Contact Us',
            'link_to' => 'contact-us.php',
            'parent' => 4,
            'level' => 3
        ),
        array(
            'id' => 6,
            'name' => 'Blog',
            'link_to' => 'blog.php',
            'parent' => 0,
            'level' => 1
        ),
    );


    $refs = array();

    foreach($arr as &$item) {
        $item['children'] = array();
        $refs[$item['id']] = $item;
    }

    unset($item); // To delete the reference

    // We define a ROOT that is the top of each elements
    $refs[0] = array(
        'id' => 0,
        'children' => array()
    );

    foreach($arr as $item) {
        if($item['id'] > 0) {
            $refs[$item['parent']]['children'][] = &$refs[$item['id']];
        }
    }

    $result = $refs[0];

    unset($refs); // To delete references

    var_dump($result);

?>
like image 174
Leto Avatar answered Sep 30 '26 07:09

Leto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!