-3

I have this array:

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

And I want to get a count of each unique value like this:

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

I really have no idea on how to achieve this, but this format is totally unknown to me.

1

2 Answers 2

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

echo json_encode($result);
Sign up to request clarification or add additional context in comments.

Comments

0

Try This:-

$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."]";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.