4

I have the following array:

$name_arr = array('raj','raj','ganesh','rahul','ganesh','mayur','raj','rahul');

I want to sort it like this:

$final_arr = array('raj','raj','raj','ganesh','ganesh','rahul','rahul','mayur');

How can I achieve it?

0

4 Answers 4

7

Simple way using array_count_values and arsort:-

$array = array_count_values($name_arr); //get all occurrences of each values
arsort($array);
print_r($array);//print occurrences array
$final_array = array();

foreach($array as $key=>$val){ // iterate over occurrences array
  for($i=0;$i<$val;$i++){ //apply loop based on occurrences number
    $final_array[] = $key; // assign same name to the final array
  }
}

print_r($final_array); // print final array

Output:- https://eval.in/847428

Sign up to request clarification or add additional context in comments.

1 Comment

This is computationally expensive - there are simpler solutions for this problem.
4

simple use array_count_values and array_fill and array_merge

1st : array_count_values will get the values presented count as a array like below .

Array ( [raj] => 3 [ganesh] => 2 [rahul] => 2 [mayur] => 1 )

2nd : Apply arsort() . descending order, according to the value

3rd : Loop that array and make the new array based on count fill the array using array_fill .

4th : Then merge the array .

<?php

$name_arr = array('raj','raj','ganesh','rahul','ganesh','mayur','raj','rahul');

$new_arr = array_count_values($name_arr);

arsort($new_arr);

$value=array();

foreach($new_arr as $key=>$val){

   $value= array_merge($value,array_fill(0,$val,$key));
}

print_r($value);

?>

4 Comments

This is computationally expensive - there are simpler solutions for this problem.
With n being the size of the array, you are invoking array_merge() and array_fill() n times.
No, but it's just not the best solution.
it's not best solution as well as it's not wrong solution . @localheinz
4

The easiest way to solve this is by using the built-in functions array_count_values() and usort():

<?php

$name_arr = array('raj','raj','ganesh','rahul','ganesh','mayur','raj','rahul');

$valueCount = array_count_values($name_arr);

$final_arr = $name_arr;

usort($final_arr, function ($a, $b) use ($valueCount) {
    return $valueCount[$b] - $valueCount[$a];
});

var_dump($final_arr);

For reference, see:

For an example, see:

4 Comments

Since you keep insisting that rebuilding the array is more expensive… not sure repeated invocation of the comparator function is any cheaper.
Even so, it's still the least amount of code while maintaining a high level of legibility, don't you think, @deceze?
I certainly agree there, and for small arrays this is preferable regardless of its performance, since it's likely fast enough.
The code does not seem to work properly – the $final_arr is not actually sorted by the number of occurrences.
-2
<?php

$name_arr = array('raj','raj','ganesh','rahul','ganesh','mayur','raj','rahul');

rsort($name_arr);

print_r($name_arr);

Output

Array (raj , raj , raj , rahul , rahul, mayur, ganesh, ganesh )

1 Comment

This might work with this particular array, but it doesn't generically solve the problem stated by the original poster. See php.net/manual/de/function.rsort.php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.