2012-10-30 131 views
1

我有一個具有兩個特定鍵/值對的數組數組。我想要做的是獲得由qty決定的前5個唯一cause值。這意味着它將按照唯一的值將cause鍵組合在一起,然後總計每個原因的qty鍵,並返回每個原因的前5個原因和總數量。數組對象前5個唯一值

這是print_r(array_values($ array))打印出來的東西。

Array ( 
[0] => Array ([cause] => Other (please comment) [qty] => 0.417) 
[1] => Array ([cause] => Chem Out FC-DryStrAddTow [qty] => 0.430) 
[2] => Array ([cause] => Chem Out FC-DryStrAddTow [qty] => 0.430) 
[3] => Array ([cause] => Chem Out FC-DryStrAddTow [qty] => 0.513) 
[4] => Array ([cause] => Chem Out FC-DryStrAddTow [qty] => 0.513) 
[5] => Array ([cause] => Chem Out FC-DryStrAddTow [qty] => 0.750) 
[6] => Array ([cause] => Chem Out FC-DryStrAddTow [qty] => 0.750) 
[7] => Array ([cause] => Slitter Cut/Saw-Poor Cut [qty] => 0.816) 
[8] => Array ([cause] => Slitter Cut/Saw-Poor Cut [qty] => 0.816) 
[9] => Array ([cause] => Slitter Cut/Saw-Poor Cut [qty] => 0.828) 
[10] => Array ([cause] => Slitter Cut/Saw-Poor Cut [qty] => 0.828) 
[11] => Array ([cause] => Slitter Cut/Saw-Poor Cut [qty] => 0.681) 
[12] => Array ([cause] => Slitter Cut/Saw-Poor Cut [qty] => 0.681) 
[13] => Array ([cause] => No Cause Selected [qty] => 0.918) 
[14] => Array ([cause] => No Cause Selected [qty] => 0.918) 
[15] => Array ([cause] => No Cause Selected [qty] => 0.926) 
[16] => Array ([cause] => No Cause Selected [qty] => 0.937) 
[17] => Array ([cause] => No Cause Selected [qty] => 0.809) 
[18] => Array ([cause] => No Cause Selected [qty] => 0.809) 
[19] => Array ([cause] => Slitter Cut/Saw-Poor Cut [qty] => 0.891) 
[20] => Array ([cause] => Slitter Cut/Saw-Poor Cut [qty] => 0.891) 
[21] => Array ([cause] => Slitter Cut/Saw-Poor Cut [qty] => 0.884) 
[22] => Array ([cause] => Slitter Cut/Saw-Poor Cut [qty] => 0.884)) 

非常感謝您的幫助。

+0

請格式化該blob的輸出,它實際上是不可讀的。 –

+0

這是一個數組與子數組,而不是對象 – JvdBerg

+0

可能的重複:http://stackoverflow.com/questions/96759/how-do-i-sort-a-multidimensional-array-in-php – bbb

回答

2
<?php 

// Total up the quantities 
$totals = array(); 
$tally = array(); 
foreach ($datapoints as $entry) { 
    if (!isset($totals[$entry['cause']]) { 
     $totals[$entry['cause']] = 0; 
     $tally[$entry['cause']] = 0; 
    } 
    $totals[$entry['cause']] += $entry['qty']; 
    $tally[$entry['cause']]++; 
} 

// Sort them, 'descending' or 'reverse', so the larger entries are first 
arsort($totals, SORT_NUMERIC); 

// Take the first 5 entries from the sorted list 
$top = array_slice($totals, 0, 5); 

// Do something with it. 
foreach ($top as $cause => $totalQty) { 
    echo $cause . ': ' . $totalQty . ' with ' . $tally[$cause] . ' events' . PHP_EOL; 
} 

如果這個數據是從數據庫中,雖然未來,這將是更好地使用數據庫和SQL,喜歡的東西:

SELECT cause, SUM(qty) as totalQty, COUNT(qty) as tally 
FROM data 
GROUP BY cause 
ORDER BY totalQty DESC 
LIMIT 5; 

而是然後加載在PHP中的所有數據和處理。

+0

這個完美的作品。非常感謝!! –

+0

我怎樣才能得到他們每個人的總數?我需要顯示每個人存在多少次。 –

+0

在php中,你可以在循環中創建一個計數散列,總計數量爲「$ tally [$ cause] ++;」或者如果它是一個選項,則在SQL中將select COUNT的'COUNT(qty)as tally'添加到select子句中。 – rrehbein

1

如果使用PHP 5.3+,你可以在你的代碼中使用閉包和排序像(由數量降序)

usort($arary,function($a,$b){ 
    return $b['qty'] - $a['qty'] 
} 

在這之後,你可以遍歷數組,並採取前5點的唯一身份。

相關問題