简体   繁体   English

按键值对数组进行排序PHP

[英]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. 因此,这些键是第一个在值中具有img的键。 And the entries without among them. 以及没有这些条目的条目。 I tried array_values but it returns the same Array. 我尝试了array_values但它返回了相同的Array。 Maybe cause the key ' Alben '? 也许会导致钥匙“ Alben ”?

I tried: 我试过了:

$returnArray = array_values($returnArray);

array_values will return you an array with just values and break your association. array_values将返回仅包含值的数组,并破坏关联。 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. 您可能需要使用uasort函数来执行此操作,在这里您可以为值提供自定义排序逻辑并维护索引关联。 From PHP Docs: 从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:) (从它的推导中得出usort :)

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. 请仔细阅读PHP Docs上的回调函数,它们可能会帮助您解决项目中的类似问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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