0

I get results from a MYSQL query, and I'm struggling turning this array into a nested list :

Array(

[0] => Array
    (
        [post_title] => Esprit
        [post_id] => 240
        [folder_id] => 236
    )

[1] => Array
    (
        [post_title] => GC
        [post_id] => 241
        [folder_id] => 236
    )

[2] => Array
    (
        [post_title] => Guess
        [post_id] => 242
        [folder_id] => 236
        [children] => Array
            (
                [0] => Array
                    (
                        [post_title] => Jewels
                        [post_id] => 250
                        [folder_id] => 242
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [post_title] => Products-shots
                                        [post_id] => 251
                                        [folder_id] => 250
                                        [children] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [post_title] => New-Collection
                                                        [post_id] => 252
                                                        [folder_id] => 251
                                                    )

                                            )

                                    )

                            )

                    )

            )

    )

[3] => Array
    (
        [post_title] => Guess-Connect
        [post_id] => 243
        [folder_id] => 236
    )

[4] => Array
    (
        [post_title] => Nautica
        [post_id] => 244
        [folder_id] => 236
    )

[5] => Array
    (
        [post_title] => Obaku
        [post_id] => 245
        [folder_id] => 236
    )

[6] => Array
    (
        [post_title] => Police
        [post_id] => 246
        [folder_id] => 236
    )

[7] => Array
    (
        [post_title] => Roamer
        [post_id] => 247
        [folder_id] => 236
    )

[8] => Array
    (
        [post_title] => Superdry
        [post_id] => 248
        [folder_id] => 236
    )

[9] => Array
    (
        [post_title] => Vulcain
        [post_id] => 249
        [folder_id] => 236
    )

)

I tried tons of codes, but none can get me this precise output :

<ul>
    <li>Esprit</li>
    <li>GC</li>
    <li>Guess
        <ul>
            <li>Jewels</li>
            <li>Products-shot</li>
            <li>New-collection</li>
        </ul>
    </li>
    <li>Guess-Connect</li>
    <li>Nautica</li>
    <li>Obaku</li>
    <li>Police</li>
    <li>Roamer</li>
    <li>Superdry</li>
    <li>Vulcain</li>
</ul>

I'd really appreciate help ! Thanks in advance

2
  • The SQL result structure is wrong. You have nested children, but you want to consider them as childrens of same parent. What is wrong? Your concept, or the database structure? Commented May 30, 2016 at 14:08
  • Actually I get unnested data from phpMyAdmin. I use a function to build up a new array nesting the children to their parents according to a 'parent_id' column. Commented May 30, 2016 at 14:23

3 Answers 3

2

You can only put second level display try this:

function second_level_list( $arr ){
  foreach( $arr as $item ){
     echo '<li>';
     echo $item['post_title'];
     echo '</li>';
     if( isset($item['children']) && is_array($item['children']) ){
       second_level_list( $item['children']);
     }
  }
}

<ul>
   <?php  foreach( $array as $list  ): ?> 
     <li><?php echo $list['post_title'] ?>
     <?php if( isset( $list['children'] ) && is_array($list['children']) ): ?>
       <ul> 
          <?php second_level_list( $list['children'] ); ?>
       </ul> 
      <?php endif; ?>  
     </li>
   <?php endforeach; ?>  
</ul>

Complete results https://3v4l.org/YD0Yc

Sign up to request clarification or add additional context in comments.

Comments

1

try the following solution:

function htmlList($arr)
{
    $list = '<ul>';
    foreach ($arr as $value)
    {
        $listItem = (is_array($value) ? htmlList($value) : $value);
        $list .= "<li>$listItem</li>";
    }
    $list .= '</ul>';
    return $list;
}
echo htmlList($array);

3 Comments

Where is ’make_list` defined?
I think answerer means htmlList(). You need to call the same function for the sublists.
htmlList($array) instead of htmlList($arr)
0

I adapted your PHP code a little bit, and seems to work for me :

function second_level_list( $arr ){
  foreach( $arr as $item ){
     echo '<li>'.$item['post_title'].'</li>';
     if( isset($item['children']) && is_array($item['children']) ){
     echo '<ul>';
       second_level_list( $item['children']);
     echo '</ul>';
     }
  }
}

Thanks mate

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.