2014-06-22 80 views
-3

我有這樣的數組:格式化一個PHP數組

array ( 
    [0] => Chrome 35 (Lin) 
    [1] => Chrome 35 (Unk) 
    [2] => Chrome 35 (Unk) 
    [3] => Chrome 18 (Lin) 
    [4]) 

而且我想這樣每個唯一值的計數:

[["Chrome 35 (lin)",1],["Chrome 35 (Unk)",2],["firefox",20]] 

我真的就如何實現這一目標不知道,但這種格式對我來說完全是未知的。

+0

http://www.php.net//manual/en/function.array-count-values.php –

回答

1
$counts = array_count_values($values); 
$result = array_map(null, array_keys($counts), $counts); 

echo json_encode($result); 
0

試試這個: -

$a = array("Chrome 35 (Lin)","Chrome 35 (Unk)", "Chrome 35 (Unk)", "Chrome 18 (Lin)", "Chrome 18 (Lin)", "Chrome 18 (Lin)", "Chrome 18 (Lin)", "Chrome 18 (Lin)"); 
$vals = array_count_values($a); 
foreach($vals as $k=>$v){ 
    $c .= '["'.$k.'", '.$v.']'; 
} 
echo "[".$c."]";