重新格式化一个php数组

时间:2014-06-22 08:54:35

标签: php arrays

我有这个数组:

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]]

我真的不知道如何实现这个目标,但这种格式对我来说完全不为人知。

2 个答案:

答案 0 :(得分:1)

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

echo json_encode($result);

答案 1 :(得分: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."]";