Php usort数组-按字母顺序列出歌曲
我写了一组函数,它将使用下面的数组,并按字母顺序对所有歌曲进行排序。问题是,它所做的只是按照艺术家在数组中的顺序吐出他们的歌曲 功能Php usort数组-按字母顺序列出歌曲,php,sorting,usort,Php,Sorting,Usort,我写了一组函数,它将使用下面的数组,并按字母顺序对所有歌曲进行排序。问题是,它所做的只是按照艺术家在数组中的顺序吐出他们的歌曲 功能 function arraySort($a, $b){ return $a['title'] > $b['title']; } function sortSongs($artist){ $count = count($artist); if($count == 2){ foreach($artist as $alb
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']);
阵列
$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'
),
),
),
);
注意:为了便于阅读,我缩短了数组
因此,我所做的就是说,如果我传入的艺术家有两张专辑,那么我们传入专辑名称,然后对该专辑的歌曲使用usort…我得到的只是我向您展示的完全相同的数组,未排序 这是非常疯狂的代码。。但是,既然有这么多嵌套数组,我真的看不到一个更好的方法不循环所有内容。过来看。理想情况下,你可以从中得到你想要的。如果你花更多的时间在这个概念上,你可以制作一些非常好的array_walk函数或类似的函数来更整洁地完成工作 主要职能: PHP
arraySort('Creed', $music);
echo '<pre>';
print_r($music);
echo '</pre>';
function arraySort($artist, &$music) {
// Validate we have an array
if(is_array($music[$artist])) {
// Sort the albums the best we can (natsort not natually available for keys)
ksort($music[$artist]);
// Loop through the artists albums
foreach($music[$artist] as $album_name => $album) {
// Loop through the songs
foreach($album as $songs)
{
// Let's build a new helper array of songs
$new_array = array();
foreach($music[$artist][$album_name] as $title)
{
$new_array[] = $title['title'];
}
// Natural sort the songs
natsort($new_array);
// Reset the Songs array
$music[$artist][$album_name] = array();
// Replace the songs as they're sorted back into the music array
foreach($new_array as $stitle)
{
$music[$artist][$album_name][] = array('title' => $stitle);
}
}
}
}
}
看起来您正在向usort传递一个包含1项的数组,即usort(数组('title'=>'Rain'),arraySort) 所以你得到的是相同的数组,因为你基本上告诉它什么也不排序 要更正此问题,您应该尝试将其发送到整个唱片集阵列,因此只传递$artist,而不是artist[$key]
作为警告,您的递归基本情况似乎非常挑剔,这不是测试是否递归的好方法。计数为2的测试基本上只在阵列与当前阵列相同的情况下才有效,这不是一般的基本情况。您在这方面是正确的,只是不需要foreach来制作相册
}感谢您提供格式正确的代码。@phpisuber01您的评论没有帮助,我也会接受您的答案……但我只能接受一个D:我希望我们可以接受主要答案,然后将次要答案作为“信息”,因为他的代码有意义,您的解释更有意义。有没有相反的方法?比如说,我怎样才能不按字母顺序反转z-a?是的,只需在
arraySort()的返回/比较行中交换$a
和$b
。
Array
(
[Creed] => Array
(
[Full Circle] => Array
(
[0] => Array
(
[title] => A Thousand Faces
)
[1] => Array
(
[title] => Away in Silence
)
[2] => Array
(
[title] => Bread of Shame
)
[3] => Array
(
[title] => Overcome
)
[4] => Array
(
[title] => Rain
)
[5] => Array
(
[title] => Suddenly
)
)
[Human Clay] => Array
(
[0] => Array
(
[title] => Are You Ready
)
[1] => Array
(
[title] => Beautiful
)
[2] => Array
(
[title] => Say I
)
[3] => Array
(
[title] => What If
)
)
)
)
function sortSongs($artist){
$count = count($artist);
if($count == 2){
foreach($artist as $album=>$trackListing){
sortSongs($artist[$album]);
}
}else{
usort($artist, 'arraySort');
print_r($artist);
}