Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested pages in Array - PHP

Tags:

arrays

php

I'm retrieving all the pages in my database for a custom CMS. The pages are nested and have parent_id to find the children of parent pages.

The code to retrieve the code is :

public function get_nested ()
{
    $pages = $this->db->get( 'pages' )->result_array();

    $array = array();
    foreach ( $pages as $page )
    {
        if ( !$page['parent_id'] )
        {
            $array[$page['id']] = $page;
        }
        else
        {
            $array[$page['parent_id']]['children'][] = $page;
        }
    }

    return $array;
}

For some reason the else condition isn't working with $array[$page['parent_id']].

A dump of $pages gives me

Array
(
    [0] => Array
        (
            [id] => 1
            [title] => Homepage
            [slug] => /
            [order] => 1
            [body] => Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do     eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat.
            [parent_id] => 0
        )

    [1] => Array
        (
            [id] => 3
            [title] => Contact
            [slug] => contact
            [order] => 0
            [body] => <p>This is my contact page.</p>
            [parent_id] => 4
        )

    [2] => Array
        (
            [id] => 4
            [title] => About
            [slug] => about
            [order] => 0
            [body] => <p>All about our company.</p>
            [parent_id] => 0
        )

)

So I am expecting contact to appear in the else part of the condition. I've tried just echoing 'parent' and 'child' in the conditions and it works but when writing $array[$page['parent_id']] nothing is appearing, not even a block saying that it's empty. The response I'm currently getting is:

Array
(
    [1] => Array
        (
            [id] => 1
            [title] => Homepage
            [slug] => /
            [order] => 1
            [body] => Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do     eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat.
            [parent_id] => 0
        )

    [4] => Array
        (
            [id] => 4
            [title] => About
            [slug] => about
            [order] => 0
            [body] => <p>All about our company.</p>
            [parent_id] => 0
        )

)

Does anyone know why $array[$page['parent_id']] isn't doing anything? I thought it would give me a children array inside [4] but nothing is there. I've checked the type of the parent_id inside the database and it's INT(11) so that shouldn't be creating the problem.

Thanks in advance.

like image 301
xonorageous Avatar asked Aug 08 '26 19:08

xonorageous


1 Answers

It will set the children in your array.. But this code has one bug. When the children appears before it's parent, it's gonna set children first but you are overwriting the the $array when parent appears after children in following condition ..

if ( !$page['parent_id'] )
{
    $array[$page['id']] = $page;
}

So it will not preserve already set children.

You have to do something like this:

if ( !$page['parent_id'] )
{
    if(!isset($array[$page['id']]))
        $array[$page['id']] = $page;
    else
       $array[$page['id']] = array_merge($page,$array[$page['id']]);
}

It will preserve the children then. Let me know if that works.


Update: I just tried to run your program with above condition... Its giving output like this:

array
  1 => 
    array
      'id' => int 1
      'title' => string 'homepage' (length=8)
      'parent_id' => int 0
  4 => 
    array
      'id' => int 4
      'title' => string 'about' (length=5)
      'parent_id' => int 0
      'children' => 
        array
          0 => 
            array
              'id' => int 3
              'title' => string 'contact' (length=7)
              'parent_id' => int 4
like image 133
V_K Avatar answered Aug 11 '26 08:08

V_K



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!