How to sort multidimensional Array a by Value in a subsub Array?

Hello everyone!
I have an Array (see below) and have to sort this array by the value of the “name” key where the “stage” key is 0. After that sort the [deck] Arrays by the value of the “name” key where the “stage” key is 1. After that sort the [deck] Arrays by the value of the “name” key where the “stage” key is 2.
Many thanks!

$all_decks = Array (
[0] => Array (
[cno] => 95
[deck] => Array (
[0] => Array ( [id] => 1565781894941 [name] => Allgemeine Pathologie [stage] => 2 [parent] => Pathologie )
[1] => Array ( [id] => 1565775443967 [name] => PM IX [stage] => 0 [superparent] => 1 [parent] => )
[2] => Array ( [id] => 1565825092761 [name] => Ernu00e4hrungspathophysiologie [stage] => 2 [parent] => Pathophysiologie )
[3] => Array ( [id] => 1565875923687 [name] => Lungenpathophysiologie [stage] => 2 [parent] => Pathophysiologie )
[4] => Array ( [id] => 1565824440541 [name] => Lipidstoffwechsel [stage] => 2 [parent] => Pathophysiologie )
[5] => Array ( [id] => 1565790017143 [name] => Pathophysiologie [stage] => 1 [parent] => PM IX )
[6] => Array ( [id] => 1565788237299 [name] => Endokrinpathologie [stage] => 2 [parent] => Pathologie )
[7] => Array ( [id] => 1565866617630 [name] => Pharmakologie [stage] => 1 [parent] => PM IX )
[8] => Array ( [id] => 1565783425425 [name] => Tumorpathologie [stage] => 2 [parent] => Pathologie )
[9] => Array ( [id] => 1565824636220 [name] => Pathopysiologie der Zelle [stage] => 2 [parent] => Pathophysiologie )
[10] => Array ( [id] => 1565883206207 [name] => Tumorpathophysiologie [stage] => 2 [parent] => Pathophysiologie )
[11] => Array ( [id] => 1565781845125 [name] => Pathologie [stage] => 1 [parent] => PM IX )
[12] => Array ( [id] => 1565781845126 [name] => Herz und Kreislauf [stage] => 2 [parent] => Pathologie )
[13] => Array ( [id] => 1565825384871 [name] => Herz und Kreislauf [stage] => 2 [parent] => Pathophysiologie )
[14] => Array ( [id] => 1565825772977 [name] => Immunsystem [stage] => 2 [parent] => Pathophysiologie ) ) )

			[1] => Array ( 
				[cno] => 96 
				[deck] => Array ( 
					[15] => Array ( [id] => 1565620405089 [name] => 02 Medical Skills 2 [stage] => 1 [parent] => Famulaturlizenz ) 
					[16] => Array ( [id] => 1565620405093 [name] => 04 Emergency Skills [stage] => 1 [parent] => Famulaturlizenz ) 
					[17] => Array ( [id] => 1565620405090 [name] => 03 Surgical Skills [stage] => 1 [parent] => Famulaturlizenz ) 
					[18] => Array ( [id] => 1565620405085 [name] => 01 Medical Skills 1 [stage] => 1 [parent] => Famulaturlizenz ) 
					[19] => Array ( [id] => 1565620405080 [name] => Famulaturlizenz [stage] => 0 [superparent] => 1 [parent] => ) ) ) )

Where does this array come from in the first place? If from a Database, this should be done in the query, not in code.

1 Like

If that’s not from a database you could still use usort() with a self-defined function utilising strnatcmp

https://www.php.net/manual/en/function.usort.php

Many thanks for your help!! The Array comes from a database, but it comes as a json-object! So unfortunately it’s not possible to change query! In the meantime, however, I have already found a solution!
Thanks again for your kind help!

Sponsor our Newsletter | Privacy Policy | Terms of Service