-1

我有一个多维关联数组,我试图按字母顺序和大小排序。检查我当前得到的示例,但现在我想按字母顺序对键“ACC,ABB,AAB”进行排序,如果有意义的话,不编辑大小排序。

arsort($myarray);

// Returns an array sorted by its value size from biggest to smallest but need to to also sort it keys alpabetically without amongest keys that have the same size.

$myArray = Array (     <--- Arrange this alphabetically and by size
        [AAA] => Array ( is 1st
            [1] => 1
            [2] => 1
            [3] => 1 
            [4] => 1
        )      
        [ACC] => Array ( should be 3rd
            [1] => 1 
            [2] => 1 
        )      
        [ABB] => Array ( should be 2nd
            [1] => 1
            [2] => 1 
        )      
        [AAB] => Array ( should be 1st
            [1] => 1 
            [2] => 1 
        )      
        [AA] => Array ( is 1st
            [1] => 1 
        )      
        [AB] => Array ( is 2nd
            [1] => 1 
        ) 
    )
4

2 回答 2

1

使用键排序对数组进行排序。根据您的要求,您希望根据键对数组进行排序这将为您工作...

ksort($myArray);

按大小排序是什么意思?我没有得到你问题的第二部分

于 2013-02-06T07:15:03.037 回答
0

最后我只是做了一个循环来做我想要的不仅仅是一个内置函数。希望它可以理解我在解释代码方面很糟糕:S

arsort($myArray);                                     // Sorts array by size
reset($myArray);
$first_key = key($myArray);
$count = count($myArray[$first_key], COUNT_RECURSIVE); // Get the largest value in array

$temp_array = array();
$sorted_array = array();
for($i = $count; $i >= 1; $i--){                      // Loops from the largest value to the smallest to create temp array for each size
    foreach($myArray as $name => $values){            // Loops Through $myArray
        if(count($myArray [$name]) == $i){            // Checks each Key in $myArray to see if size is the same as $I
            $temp_array[$name] = $myArray[$name];     // Adds to $temp_array
        }

    }
    ksort($temp_array);                               // Sorts temp array created in loop above alpahbetically
    $sorted_array += $temp_array;                     // and adds its to a new array
    $temp_array = array();
}

此循环允许您按数组值的大小排序,然后如果有多个具有相同值的键将按字母顺序排序

$myArray = Array (    // This array sorted by size only - arsort($myArray)
    [AB] => Array (
        [1] => 1
        [2] => 1
        [3] => 1 
    )      
    [AA] => Array (
        [1] => 1 
        [2] => 1
        [3] => 1 
    )
    [AC] => Array (
        [1] => 1 
        [2] => 1
        [3] => 1 
    )      
    [BB] => Array (
        [1] => 1
        [2] => 1 
    )      
    [BA] => Array (
        [1] => 1 
        [2] => 1 
    )      
    [CA] => Array (
        [1] => 1 
    )      
    [CB] => Array (
        [1] => 1 
    ) 
)

$myArray = Array (     // Array Sorted By Size and Alphabetically - After loop
    [AA] => Array (
        [1] => 1
        [2] => 1
        [3] => 1 
    )      
    [AB] => Array (
        [1] => 1 
        [2] => 1
        [3] => 1 
    ) 
    [AC] => Array (
        [1] => 1 
        [2] => 1
        [3] => 1 
    )      
    [BA] => Array (
        [1] => 1
        [2] => 1 
    )      
    [BB] => Array (
        [1] => 1 
        [2] => 1 
    )      
    [CA] => Array (
        [1] => 1 
    )      
    [CB] => Array (
        [1] => 1 
    ) 
)
于 2013-02-06T23:50:52.083 回答