<?php $arr = [ [ "id" => "127", "title" => "Apple", "deleted" => "0", "parent" => NULL, "usp_id" => "445", "user_id" => NULL, ], [ "id" => "159", "title" => "Renault Duster", "deleted" => "0", "parent" => NULL, "usp_id" => "495", "user_id" => NULL, ], [ "id" => "7", "title" => "Services", "deleted" => "0", "parent" => "127", "usp_id" => "79", "user_id" => "275", ], ]; foreach ($arr as $key0 => $record) { if (null !== $record['parent']) { foreach ($arr as $key1 => $parent) { if ($parent['id'] === $record['parent']) { $arr[$key1]['children'][] = $record; // push child into parent['sub'] unset($arr[$key0]); // delete child } } } } print_r($arr);
You have javascript disabled. You will not be able to edit any code.