简体   繁体   中英

Sort Array by key value PHP

I have this Array:

Array
(
    [Alben] => Array
        (
            [Sgt._Pepper’s_Lonely_Hearts_Club_Band] => 
            [Rubber_Soul] => 
            [Please_Please_Me] => 
            [Let_It_Be_(Album)] => 
            [With_the_Beatles] => 
            [Let_It_Be…_Naked] => 
            [A_Hard_Day’s_Night_(Album)] => Rickenbacker_360-12WB_12_String.jpg
            [Beatles_for_Sale] => 
            [Help!_(Album)] => Semaphore_Victor.svg
            [Magical_Mystery_Tour_(Album)] => 
            [The_Beatles_(Album)] => 
            [Yellow_Submarine] => 
            [Abbey_Road] => The_End.jpg
            [Revolver_(Album)] => 
            [Anthology_(The_Beatles)] => 
            [A_Collection_of_Oldies_…_but_Goldies] => 
            [1_(Album)] => 
        )

)

Now I want to sort it by the value. So, that the keys are first which have an img in the value. And the entries without among them. I tried array_values but it returns the same Array. Maybe cause the key ' Alben '?

I tried:

$returnArray = array_values($returnArray);

array_values will return you an array with just values and break your association. You may need to use the uasort function to do this, where you can provide a custom sorting logic to your values and maintain index association. From PHP Docs:

This function sorts an array such that array indices maintain their correlation with the array elements they are associated with, using a user-defined comparison function.

This is used mainly when sorting associative arrays where the actual element order is significant.

(from it's derivation usort comes:)

value_compare_func

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

Here's an example upon the code you provided:

<?php
// array reproducing
$collection = array(
        "Alben" => array (
                "Sgt._Pepper’s_Lonely_Hearts_Club_Band" => "",
                "Rubber_Soul" => "",
                "Please_Please_Me" => "",
                "Let_It_Be_(Album)" => "",
                "With_the_Beatles" => "",
                "Let_It_Be…_Naked" => "",
                "A_Hard_Day’s_Night_(Album)" => "Rickenbacker_360-12WB_12_String.jpg",
                "Beatles_for_Sale" => "",
                "Help!_(Album)" => "Semaphore_Victor.svg",
                "Magical_Mystery_Tour_(Album)" => "",
                "The_Beatles_(Album)" => "ble_test",
                "Yellow_Submarine" => "",
                "Abbey_Road" => "The_End.jpg",
                "Revolver_(Album)" => "",
                "Anthology_(The_Beatles)" => "",
                "A_Collection_of_Oldies_…_but_Goldies" => "",
                "1_(Album)" => "",
        ));

// callback of sorting, checks if the compared value is not empty then pushes it ahead.
// If not, pull it backwards.
$sortIfFilled = function ($a, $b) {
    return (!$a) ? 1 : -1;
};

// Executing it...
uasort($collection['Alben'], $sortIfFilled);

echo "<pre>";
print_r ($collection);

OUTPUT:

Array
(
    [Alben] => Array
        (
            [The_Beatles_(Album)] => ble_test
            [Abbey_Road] => The_End.jpg
            [A_Hard_Day’s_Night_(Album)] => Rickenbacker_360-12WB_12_String.jpg
            [Help!_(Album)] => Semaphore_Victor.svg
            [Yellow_Submarine] => 
            [Let_It_Be…_Naked] => 
            [A_Collection_of_Oldies_…_but_Goldies] => 
            [Let_It_Be_(Album)] => 
            [Revolver_(Album)] => 
            [Beatles_for_Sale] => 
            [With_the_Beatles] => 
            [Sgt._Pepper’s_Lonely_Hearts_Club_Band] => 
            [Rubber_Soul] => 
            [Magical_Mystery_Tour_(Album)] => 
            [Anthology_(The_Beatles)] => 
            [Please_Please_Me] => 
            [1_(Album)] => 
        )

)

Consider a careful reading of callback functions on PHP Docs, they might help you with similar issues along your projects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM