按键值PHP排序数组

时间:2015-09-28 14:13:04

标签: php arrays sorting

我有这个数组:

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)] => 
        )

)

现在我想按值对其进行排序。 因此,键首先在值中具有img。并且没有他们之间的条目。我尝试了array_values,但它返回相同的数组。也许会导致关键' Alben'?

我试过了:

$returnArray = array_values($returnArray);

1 个答案:

答案 0 :(得分:3)

array_values会返回一个只包含值的数组并破坏您的关联。您可能需要使用uasort函数来执行此操作,您可以在其中为值提供自定义排序逻辑并维护索引关联。 来自PHP文档:

  

此函数对数组进行排序,使数组索引保持其状态   与它们相关的数组元素的相关性,使用a   用户定义的比较函数。

     

这主要用于排序实际的关联数组   元素顺序很重要。

(来自它的推导usort来了:))

  

<强> value_compare_func

     

如果认为第一个参数是,则比较函数必须返回小于,等于或大于零的整数   分别小于,等于或大于第二个。

以下是您提供的代码示例:

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

<强>输出:

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)] => 
        )

)

考虑仔细阅读PHP Docs上的回调函数,它们可能会帮助您解决项目中的类似问题。