2017-03-09 300 views
0

不知道我錯過了某些顯而易見的事情,但我無法在其他答案中找到排序多維數組子組的方法,而無需觸摸主要順序。僅對多維數組進行PHP排序子組

我有一個數組這樣

Array 
(
    [1] => Array 
     (
      [name] => Apples 
      [type] => 1 
      [sales] => 10 
     ) 

    [2] => Array 
     (
      [name] => Apples 
      [type] => 2 
      [sales] => 30 
     ) 

    [3] => Array 
     (
      [name] => Apples 
      [type] => 3 
      [sales] => 20 
     ) 

    [4] => Array 
     (
      [name] => Oranges 
      [type] => 1 
      [sales] => 30 
     ) 

    [5] => Array 
     (
      [name] => Oranges 
      [type] => 2 
      [sales] => 10 
     ) 

    [6] => Array 
     (
      [name] => Oranges 
      [type] => 3 
      [sales] => 20 
     ) 

    [7] => Array 
     (
      [name] => Lemons 
      [type] => 1 
      [sales] => 10 
     ) 

    [8] => Array 
     (
      [name] => Lemons 
      [type] => 2 
      [sales] => 30 
     ) 

    [9] => Array 
     (
      [name] => Lemons 
      [type] => 3 
      [sales] => 20 
     ) 
) 

我需要做的就是通過密鑰「銷售」排序每個「名」組(蘋果,橙子和檸檬),剩下的鍵「名稱的順序「蘋果,橙子和檸檬不變,所以結果應該是這樣的:

Array 
(
    [1] => Array 
     (
      [name] => Apples 
      [type] => 1 
      [sales] => 10 
     ) 

    [2] => Array 
     (
      [name] => Apples 
      [type] => 3 
      [sales] => 20 
     ) 

    [3] => Array 
     (
      [name] => Apples 
      [type] => 2 
      [sales] => 30 
     ) 

    [4] => Array 
     (
      [name] => Oranges 
      [type] => 2 
      [sales] => 10 

     ) 

    [5] => Array 
     (
      [name] => Oranges 
      [type] => 3 
      [sales] => 20 
     ) 

    [6] => Array 
     (
      [name] => Oranges 
      [type] => 1 
      [sales] => 30 
     ) 

    [7] => Array 
     (
      [name] => Lemons 
      [type] => 1 
      [sales] => 10 
     ) 

    [8] => Array 
     (
      [name] => Lemons 
      [type] => 3 
      [sales] => 20 
     ) 

    [9] => Array 
     (
      [name] => Lemons 
      [type] => 2 
      [sales] => 30 
     ) 
) 

我找不到任何方式做到這一點,任何人都可以給我一個關於這個提示?

謝謝!

+2

'usort','usort','首先的usort' –

+0

@gege安排你的數組中的PHP的形式,告訴你試過嗎? –

+1

您發佈的「代碼」中的數組結構不清楚。請發佈一個有效的php arrray定義。 – arkascha

回答

1

@gege試試這個:

<?php 
    $arr = array(
       array(
         "name" => "Apples", 
         "type" => 1, 
         "sales" => 10 
        ), 
       array(
         "name" => "Apples", 
         "type" => 2, 
         "sales" => 30 
        ), 
       array(
         "name" => "Apples", 
         "type" => 3, 
         "sales" => 20 
        ), 
       array(
         "name" => "Oranges", 
         "type" => 1, 
         "sales" => 30 
        ), 
       array(
         "name" => "Oranges", 
         "type" => 2, 
         "sales" => 10 
        ), 
       array(
         "name" => "Oranges", 
         "type" => 3, 
         "sales" => 20 
        ), 
       array(
         "name" => "Lemons", 
         "type" => 1, 
         "sales" => 10 
        ), 
       array(
         "name" => "Lemons", 
         "type" => 2, 
         "sales" => 30 
        ), 
       array(
         "name" => "Lemons", 
         "type" => 3, 
         "sales" => 20 
        ) 
      ); 
    echo "<pre>"; 
    print_r($arr); // array before 

    function sortArr($a, $b){ 
     if($a["name"] == $b["name"]){ 
      if($a["sales"] == $b["sales"]){ 
       return 0; 
      } 
      return($a["sales"] < $b["sales"] ? -1 : 1); 
     } 
     else{ 
      return($a["name"] < $b["name"] ? -1 : 1); 
     } 
    } 

    usort($arr, "sortArr"); 
    echo "<pre>"; 
    print_r($arr); // array after 
+0

謝謝你這個作品!我現在必須明白它是如何實現的,不管怎樣,PHP排序數組的方式似乎讓我感到困惑,我沒有考慮像在兩個層次上使用它。很棒,現在有時間學習並理解它,謝謝! – gege

+0

歡迎您:-) –

1

爲了便於閱讀,我減少了你的數組(它應該仍然適用於你的)。

<?php 
// The array structured as assumed according to your example 
$groceries = array(
    array(
     "name" => "Apples", 
     "type" => 1, 
     "sales" => 10 
    ), 
    array(
     "name" => "Apples", 
     "type" => 2, 
     "sales" => 30 
    ), 
    array(
     "name" => "Apples", 
     "type" => 3, 
     "sales" => 20 
    ), 
); 


/** 
* See the PHP docs for more information: http://php.net/manual/en/function.usort.php 
*/ 
$sortBySales = function ($a, $b){ 
    return $a['sales'] - $b['sales']; 
}; 


echo "<h1>Unsorted</h1>"; 
echo "<pre>"; 
    var_dump($groceries); 
echo "</pre>"; 

usort($groceries, $sortBySales); 

echo "<h1>Sorted</h1>"; 
echo "<pre>"; 
    var_dump($groceries); 
echo "</pre>"; 

編輯

我忘了,該陣列應該有名稱分組爲好。現在,@BunkerBoy alread張貼的解決方案,但對於completness的緣故,這裏是更新排序功能:

$sortBySales = function ($a, $b){ 
    if($a["name"] == $b["name"]){ 
     return $a['sales'] - $b['sales']; 
    }else{ 
     return($a["name"] < $b["name"] ? -1 : 1); 
    } 
}; 

注意,要@BunkerBoy的功能,唯一的區別是,我減少了比較的計算。

+0

嗨,感謝您的建議,但它不起作用,因爲我的主要需求是將子項「名稱」分組。按照我的示例展開數組並使用您的代碼結果並不是我所需要的,因爲它會改變「蘋果」和「桔子」的混合順序。我需要所有'蘋果'和所有'桔子'按照原始順序(所有蘋果,然後是所有桔子,然後是所有檸檬)一個接一個地排序,但是按銷售排序這些組中的每一個,所以首先通過銷售,然後是所有的橙子,然後是所有的檸檬(嘗試將你的代碼應用到我的原始數組,你會看到與我需要的結果不同)。 – gege

+0

噢,是的,對不起,我忘了 - 但似乎@BunkerBoy已經解決了這個問題:-) – mcuenez

+1

謝謝你,你的變化將有助於理解過程,因爲它產生相同的結果,以一種稍微不同的方式! :) – gege