https://stackoverflow.com/questions/49794835/foreach-array-error-php - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.

Name: https://stackoverflow.com/questions/49794835/foreach-array-error-php fullscreencopydownloadembedprint


Your result can be seen below.

Result of php executing





Full code of https://stackoverflow.com/questions/49794835/foreach-array-error-php.php

  1. <?php
  2. $menus = [
  3.         [
  4.             "id" => 1, 
  5.             "label" => "content", 
  6.             "parent_id" => 0
  7.         ],
  8.         [
  9.             "id" => 2,
  10.             "label" => "inbox", 
  11.             "id" => 3, 
  12.             "parent_id" => 0
  13.         ], 
  14.         [
  15.             "id" => 4, 
  16.             "label" => "item", 
  17.             "parent_id" => 0
  18.         ]
  19.     ];
  20. $sub_menus = [
  21.         [
  22.             "id" => 5,
  23.             "label" => "banner",
  24.             "parent_id" => 1
  25.         ],
  26.         [
  27.             "id" => 6,
  28.             "label" => "ads",
  29.             "parent_id" => 1
  30.         ]
  31.     ];
  32.  
  33. $nav = array();
  34. foreach($menus as $row => $value){
  35.     $nav[$row] = $value;
  36.     foreach($sub_menus as $r => $v) {
  37.         if($v['parent_id'] == $value['id']){
  38.             $nav[$row]['sub_menu'][$r] = $v;
  39.         }
  40.      }
  41.  }
  42.  print_r($nav);
File Description
  • https://stackoverflow.com/questions/49794835/foreach-array-error-php
  • PHP Code
  • 12 Apr-2018
  • 839 Bytes
You can Share it: