P粉1327308392023-08-23 17:06:46
The best approach is if you have control over building the initial array, then just set it up at the beginning when adding entries.
If there is no control, a temporary array is constructed for sorting:
foreach ($input_arr as $key => &$entry) { $level_arr[$entry['level']][$key] = $entry; }
This gives you the form you want, with all the references together.
If possible, initially build the array like this.
P粉9981006482023-08-23 15:20:11
First, you need to group them by level
Use foreach to loop through the array, check if the level is the same as the previous item, then group it with that array
$templevel=0; $newkey=0; $grouparr[$templevel]=""; foreach ($items as $key => $val) { if ($templevel==$val['level']){ $grouparr[$templevel][$newkey]=$val; } else { $grouparr[$val['level']][$newkey]=$val; } $newkey++; } print($grouparr);
print($grouparr);The output will be displayed in the format you want
You can also try
print($grouparr[7]);
will be displayed
[7] => Array ( [4] => Array ( [cust] => XT7434 [type] => standard ) )
or
print($grouparr[3]);
will be displayed
[3] => Array ( [2] => Array ( [cust] => XT8922 [type] => premier ) [3] => Array ( [cust] => XT8816 [type] => permier ) )