So I have this set of functions that I wrote that will take the array bellow and sort all the songs alphabetically. The problem is that all it does is spit out the songs of the artist in the same order they were placed in the array.
The functions
function arraySort($a, $b){
return $a['title'] > $b['title'];
}
function sortSongs($artist){
$count = count($artist);
if($count == 2){
foreach($artist as $album=>$trackListing){
sortSongs($artist[$album]);
}
}else{
foreach($artist as $key=>&$value){
usort($artist[$key], 'arraySort');
print_r($artist);
}
}
}
sortSongs($music['Creed']);
The Array
$music = array(
'Creed' => array(
'Human Clay' => array(
array(
'title' => 'Are You Ready'
),
array(
'title' => 'What If'
),
array(
'title' => 'Beautiful'
),
array(
'title' => 'Say I'
),
),
'Full Circle' => array(
array(
'title' => 'Overcome'
),
array(
'title' => 'Bread of Shame'
),
array(
'title' => 'A Thousand Faces'
),
array(
'title' => 'Suddenly'
),
array(
'title' => 'Rain'
),
array(
'title' => 'Away in Silence'
),
),
),
);
Note: I shortened the array for reading purposes.
So all that I am doing is saying, if the artist I pass in has 2 albums, then we pass the album name in and then use usort on the songs of that album….All I get back is the exact same array I showed you, unsorted.
You are right on track there, just don’t need foreach for album there.
}