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.

function sortSongs($artist){
$count = count($artist);
if($count == 2){
    foreach($artist as $album=>$trackListing){
        sortSongs($artist[$album]);
    }
}else{
       usort($artist, 'arraySort');
       print_r($artist);
}

}

其他提示

It looks like you are passing usort an array of 1 item, i.e. usort(array('title' => 'Rain'), arraySort);

So you're getting the same array back, because you're essentially telling it to sort nothing.

To correct the problem, you should try sending it the whole album array, so instead of artist[$key], pass it just $artist.

And as a warning your recursion base case seems very finicky, it's not a great way to test whether to recurse or not. Testing for a count of 2 will basically only work in cases where you have an array identical to the one you present, it's not a general base case.

This is pretty crazy code.. But since you have so many nested arrays I don't really see a better way without looping over everything. Check it out. Ideally, you can take from this what you want. If you spend more time on the concept you could make some very nice array_walk functions or similar to complete the work more neatly.

Key Functions:

natsort

ksort

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);
                }
            }
        }
    }
}

Output

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
                        )

                )

        )

)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top