Reputation: 1069
I have an array which contains duplicate values. I want to sort the array so that the values with the most duplicates appear first in line.
I want an output like:
Rihanna
U2
Becca
Taylor Swift
My file that contains data:
rihanna
rihanna
rihanna
rihanna
taylor swift
becca
becca
u2
u2
u2
My code as is which wont work:
$input = file_get_contents('files');
$input = explode("\n", $input);
$acv = array_count_values($input);
$acv = arsort($acv);
$result = array_keys($acv);
print_r($acv); //Outputs Blank
Upvotes: 0
Views: 238
Reputation: 2474
Here is your solution your array is :
$arr = array('rihanna','rihanna','rihanna','rihanna','taylor swift','becca','becca','u2','u2','u2');
$acv = array_count_values($input);
// If need to remove element with count = 1
foreach($acv as $key => $value)
{
echo $value;
if($value == 1)
{
unset($nArr[$key]);
}
}
//End
$fArr = array_flip($acv);
krsort($fArr);
print_r(array_values($fArr));
//output
Array
(
[0] => rihanna
[1] => u2
[2] => becca
[3] => taylor swift
)
Upvotes: 1