2013-12-17 89 views
2

我有一個數組,我想聚集每個單詞,並指望他們,就像這樣:總結和計數的數組

Array 
    (
     [0] => Notice 
     [1] => co 
     [2] => co 
     [3] => Notice 
     [4] => co 
     [5] => co 
     [6] => sls 
    ) 

應該是這樣的:

Array 
(
    [Notice] => 2 
    [co] => 4 
    [sls] => 1 
) 

什麼是ggod的方式來做到這一點?

感謝

回答

4

你需要看看array_count_values()

<?php 
$array = array(1, "hello", 1, "world", "hello"); 
print_r(array_count_values($array)); 
?> 

OUTPUT:

Array 
(
    [1] => 2 
    [hello] => 2 
    [world] => 1 
) 
2

嘗試這樣的:

<?php 
$array = array("Notice", "co", "co", "Notice", "co", "co", "sls"); 
print_r(array_count_values($array)); 
?> 

輸出

Array 
(
    [Notice] => 2 
    [co] => 4 
    [sls] => 1 
) 

欲瞭解更多信息:http://www.php.net/manual/en/function.array-count-values.php

+0

無法找到答案笑之間的相似;) –

+0

@ShankarDamodaran:現在它的呼叫勺子餵養。 –