Will it do it?
$array1 = array_count_values($array);
arsort($array1);
var_dump($array1);
will provide you
array(3) {
["foo"]=>
int(3)
["item"]=>
int(2)
["bar"]=>
int(1)
}
or do you necessarily need them as duplicate values? if so, you can go for something like:
usort($array,create_function('$a,$b',
'return $GLOBALS["array1"][$a]<$GLOBALS["array1"][$b];'));
This is ugly code, but demonstrates the technique. It's also easy to make this beautiful with php 5.3 closing, but I don't know if you are on 5.3. It will look like this:
$acount=array_count_values($array = array("foo", "bar", "item", "item", "foo", "foo"));
usort($array,function($a,$b) use ($acount) { return $acount[$a]<$acount[$b]; });
source
share