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.
Result of php executing
Full code of https://stackoverflow.com/questions/49794835/foreach-array-error-php.php
- <?php
- $menus = [
- [
- "id" => 1,
- "label" => "content",
- "parent_id" => 0
- ],
- [
- "id" => 2,
- "label" => "inbox",
- "id" => 3,
- "parent_id" => 0
- ],
- [
- "id" => 4,
- "label" => "item",
- "parent_id" => 0
- ]
- ];
- $sub_menus = [
- [
- "id" => 5,
- "label" => "banner",
- "parent_id" => 1
- ],
- [
- "id" => 6,
- "label" => "ads",
- "parent_id" => 1
- ]
- ];
- $nav = array();
- foreach($menus as $row => $value){
- $nav[$row] = $value;
- foreach($sub_menus as $r => $v) {
- if($v['parent_id'] == $value['id']){
- $nav[$row]['sub_menu'][$r] = $v;
- }
- }
- }
- print_r($nav);