I have 2 arrays of unknown length/depth that look something like this
[1] => Hand Tools
[2] => Power Tools
[4] => ╚═►Outdoor Tools
[6] => ╚═►dvjdg
[5] => ╚═►Indoor Tools
[7] => ╚═►blaha
[8] => ╚═►blahb
[3] => Garden Tools
and
[1] => 0
[2] => 0
[4] => 1
[6] => 2
[5] => 1
[7] => 2
[8] => 2
[3] => 0
Both arrays use a category id as the array index and the second array contains the depth of each category. The first array is used to generate a HTML <select> input but I now need to alphabetize this array while maintaining the proper category hierarchy. So I would need to get an output array like this.
[3] => Garden Tools
[1] => Hand Tools
[2] => Power Tools
[5] => ╚═►Indoor Tools
[7] => ╚═►blaha
[8] => ╚═►blahb
[4] => ╚═►Outdoor Tools
[6] => ╚═►dvjdg
How can I accomplish this?
Considering that
$listis your array, you can not easily sort it because each “sub”-element does not carry the information (text) of it’s parent. So first thing is to add that information:And you’ve got the sortkey. Do this for all list entries and you’ve got all sortkeys:
This example simplifies to obtain the level information. I have counted one space per level. You might have different and use a different function than
strspnfor that. Most likely you will use your second array for that. I was too lazy to type it, so I did it with the function. Your might just be:Now you can sort the
$listarray based on the$sortkeysarray. That can be done with PHPs’array_multisortfunction. Because that function would re-number numerical keys, we sort the keys as well and then combine after the sorting to preserve them:Which gives you:
Watch the full example running: Demo