Maruf Ahmed
Maruf Ahmed

Reputation: 115

Nestable menu json error PHP

I've a database table called menu_short & I've insert json data in m_short column:

[{"id":1},{"id":2},{"id":3,"children":[{"id":4},{"id":5}]}]

I'm using nestable menu for parent child menu. I use this code to extract:

foreach ($menuDisplay as $menus) {
    $json = $menus['m_short'];
    $array = json_decode($json, true);
    foreach ($array['id'] as $k => $v) {
       echo $v;
    }
}

I'm getting this 'Undefined index: id & Invalid argument supplied for foreach()' error. I'm sure I made some mistake.

DB Picture- enter image description here

Upvotes: 0

Views: 53

Answers (1)

Praveen Kumar
Praveen Kumar

Reputation: 2408

Something like this should work....

 foreach ($menuDisplay as $menus){
        $json = $menus['m_short'];
        $arrayx = json_decode($json, true);
        foreach ($arrayx as $k1 => $array) {
          var_dump($array['id']);
          /*foreach ($array['id'] as $k2 => $v) {
                 echo $v;
              }*/
        }
 }

Upvotes: 1

Related Questions