Welcome Guest, Not a member yet? Register   Sign In
Convert array structure for ul() codeigniter helper
#1

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.
Reply
#2

This is for a Category Menu but sould work just fine for what you want.

CodeIgniter 4 - Infinite dynamic Multi-level nested category with Codeigniter and MySQL
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(12-25-2022, 10:19 PM)DaveenDomsiline Wrote: I want to ask how to convert an array like below:

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.

This is a general coding task, CI does not contain any special functions for that
You have to use `for` loops and auxiliary arrays to build the resulting array
Reply
#4

(This post was last modified: 12-27-2022, 03:55 AM by luckmoshy.)

(12-26-2022, 11:37 AM)ruslan Wrote:
(12-25-2022, 10:19 PM)DaveenDomsiline Wrote: I want to ask how to convert an array like below:

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

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

This is a general coding task, CI does not contain any special functions for that
You have to use `for` loops and auxiliary arrays to build the resulting array

alright, I agree with you 100% that is why CI4 has given the freedom to whoever wants to add his/her own way plugin to output its function in whatever way he/she this this is what is called a simple, fast framework so 100% CI4 can not accomplish its engine like other like laravel and so on and this is deliberated in CI4 to make it fast, otherwise you should add your own whether is helper or function
Codeigniter First, Codeigniter Then You!!
yekrinaDigitals

Reply
#5

Indeed, CodeIgniter 4 focuses on simplicity and speed. While it may not have all the features of other frameworks like Laravel, it allows developers the freedom to add their own custom plugins and output functions in whatever way they see fit. This flexibility is a strength of the framework and allows for more tailored and efficient solutions.
Reply
#6

To convert the given array to the desired format, you can loop through each element of the array and check the 'parent' and 'level' values to determine its position in the final array. Here's some sample code that should do the job:


$result = array();

foreach ($arr as $item) {
if ($item['parent'] == 0) {
$result[$item['name']] = array();
$current_level = 1;
} else {
$parent =& $result;
$parents = array_slice($item, 3);
foreach ($parents as $parent_id) {
$parent =& $parent[$parent_id]['children'];
}
$parent[$item['name']] = array();
$current_level = count($parents) + 1;
}
while ($current_level < $item['level']) {
$parent =& $parent[$item[$current_level - 1]]['children'];
$current_level++;
}
$parent[$item['name']] = array();
}

$result = array_keys($result);
The above code will create the desired array structure and store it in the $result variable. Finally, to get the exact output you're looking for, you can use the array_keys function to extract the top-level keys from the $result array.

Hope this helps! Let me know if you have any questions.
Reply
#7

(This post was last modified: 04-11-2023, 10:31 AM by gosocial2.)

You could achieve the desired result by using a recursive function that converts the given array to a hierarchical structure. Here's a solution:

PHP Code:
function buildTree(array $elements$parentId 0) {
    $branch = array();

    foreach ($elements as $element) {
        if ($element['parent'] == $parentId) {
            $children buildTree($elements$element['id']);
            if ($children) {
                $branch[$element['name']] = $children;
            } else {
                $branch[] = $element['name'];
            }
        }
    }

    return $branch;
}

$arr = array(
    // Your original array here
);

$result buildTree($arr); 

You can wrap the buildTree function in your own CI4 helper.

CodeIgniter Wizard (CRUD code generator for Mac) instantly scaffolds Bootstrap-based web applications with an administrative interface (admin templates include Bootstrap5)

Reply
#8

To convert the given array into a nested array as required, you can use a recursive function to loop through the original array and build the new array. Here's an example function that does this:

function build_nested_array($arr, $parent_id = 0) {
$result = array();
foreach ($arr as $item) {
if ($item['parent'] == $parent_id) {
$name = $item['name'];
$children = build_nested_array($arr, $item['id']);
if (!empty($children)) {
$result[$name] = $children;
} else {
$result[] = $name;
}
}
}
return $result;
}
You can call this function with the original array as the first argument, and the parent ID (which defaults to 0) as the second argument. It will return the new nested array.

Example usage:

scss
Copy code
$result = build_nested_array($arr);
print_r($result);
This should output the following:


Array
(
[Home] =>
[About] => Array
(
[About Me] =>
[About Us] => Array
(
[Contact Us] =>
)

)

[Blog] =>
)
You can then use this new nested array to create your list using the ul() function from the CodeIgniter helper.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB