3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = array( "artists" => array( "1" => array( "id" => "1", "firstname" => "John", "lastname" => "Doe", "songs" => array( array("name" => "song_1", "year" => "1999"), array("name" => "song_2", "year" => "1987") ), ), "2" => array( "id" => "2", "firstname" => "Jane", "lastname" => "Doe", "songs" => array( array("name" => "song_1", "year" => "2000"), array("name" => "song_2", "year" => "1999") ) ), "3" => array( "id" => "3", "firstname" => "Mark", "lastname" => "Doe", "songs" => array( array("name" => "song_1", "year" => "1984"), array("name" => "song_2", "year" => "1983") ) ) ) ); array_walk($arr['artists'], function (&$song) { usort($song['songs'], function ($a, $b) { return $a['year'] > $b['year'] ? 1 : -1; }); // Ascending order //usort($song['songs'], function ($a, $b) { return $a['year'] < $b['year'] ? 1 : -1; }); // Descending order }); print('<pre>'); print_r($arr); print('</pre>');
Output for 7.1.25 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4, 8.3.6
<pre>Array ( [artists] => Array ( [1] => Array ( [id] => 1 [firstname] => John [lastname] => Doe [songs] => Array ( [0] => Array ( [name] => song_2 [year] => 1987 ) [1] => Array ( [name] => song_1 [year] => 1999 ) ) ) [2] => Array ( [id] => 2 [firstname] => Jane [lastname] => Doe [songs] => Array ( [0] => Array ( [name] => song_2 [year] => 1999 ) [1] => Array ( [name] => song_1 [year] => 2000 ) ) ) [3] => Array ( [id] => 3 [firstname] => Mark [lastname] => Doe [songs] => Array ( [0] => Array ( [name] => song_2 [year] => 1983 ) [1] => Array ( [name] => song_1 [year] => 1984 ) ) ) ) ) </pre>
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 <pre>Array ( [artists] => Array ( [1] => Array ( [id] => 1 [firstname] => John [lastname] => Doe [songs] => Array ( [0] => Array ( [name] => song_2 [year] => 1987 ) [1] => Array ( [name] => song_1 [year] => 1999 ) ) ) [2] => Array ( [id] => 2 [firstname] => Jane [lastname] => Doe [songs] => Array ( [0] => Array ( [name] => song_2 [year] => 1999 ) [1] => Array ( [name] => song_1 [year] => 2000 ) ) ) [3] => Array ( [id] => 3 [firstname] => Mark [lastname] => Doe [songs] => Array ( [0] => Array ( [name] => song_2 [year] => 1983 ) [1] => Array ( [name] => song_1 [year] => 1984 ) ) ) ) ) </pre>

preferences:
182.62 ms | 409 KiB | 181 Q