How to order a php array based on duplicate values

Anderson Montes De oca

I need to sort an array by quantity of duplicate values.

Here's an example:

$arr = array(
    1=> 'Love is true',
    2=> 'Love is true',
    3=> 'Hello Word',
    4=> 'Hello Word',
    5=> 'Hope',
    6=> 'Hope',
    7=> 'Love is true',
    8=> 'Hello Word',
    9=> 'Hello Word',
    10=>'Hope',
    11=>'Hello Word',
    12=>'Hope',
    13=>'Hello Word',
    14=>'Hello Word',
    15=>'Hello Word');
print_r($arr);

In this array, we can see that

Love is true > duplicate > 3x
Hello Word   > duplicate > 8x
Hope         > duplicate > 4x

I'd like the sorting to put the values that repeat the most first:

Hello Word position 1# In array (repeats 8 times)
Hope                2# In array (repeats 4 times)
Love is true        3# In array (repeats 3 times)

So it returns this array:

Array
(
    [0] => Hello Word
    [1] => Hope 
    [2] =  Love is true
)
FirstOne

You can do it like this:

$count = array_count_values($arr); // count each repetition
arsort($count); // sort the values
$array = array_keys($count); // get the expected array
print_r($array);

That will output:

Array
(
    [0] => Hello Word
    [1] => Hope
    [2] => Love is true
)

See it in action here.


You can get some references for the functions used in this answer below:

  • array_count_values: Counts all the values of an array;
  • asort: Sort an array and maintain index association;
  • array_keys: Return all the keys or a subset of the keys of an array.


You can var_dump each step of the process to see what's going on.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to remap array keys (based on values) with PHP?

From Dev

Remove duplicate values from a multidimensional array in PHP

From Dev

Convert numpy.array to order of elements when duplicate values are present

From Dev

Java how to sort string array based on order values were placed?

From Dev

How to avoid duplicate values in an array of arrays?

From Dev

PHP - How to test a multidimensional array for duplicate element values in any order

From Dev

Sort array in ascending order and remove duplicate values in objective- c

From Dev

How to sort a numpy array into a specific order, specified by a separate list, based on the relative sizes of the values in the array

From Dev

PHP multidimensional array, average of duplicate values

From Dev

PHP multiple duplicate values in multidimensional array

From Dev

In PHP Add the duplicate values in array

From Dev

Count of duplicate values in array php

From Dev

PHP Search Multidimensional Array for Duplicate Values

From Dev

How to order the values a pandas data frame based on the predefined array containing the desired order?

From Dev

Remove duplicate values from a multidimensional array in PHP

From Dev

Count duplicate values in PHP array

From Dev

Java how to sort string array based on order values were placed?

From Dev

How can I order an array with duplicate values?

From Dev

PHP - How to test a multidimensional array for duplicate element values in any order

From Dev

Php merge duplicate array values in a multidimensional array php

From Dev

PHP multiple duplicate values in multidimensional array

From Dev

In PHP Add the duplicate values in array

From Dev

Merge arrays based on duplicate values php

From Dev

How to remove duplicate values based on multiple columns

From Dev

How to order a php array based on duplicate values

From Dev

Custom order for php array based on indexes

From Dev

How to sort instances of a class based in ascending order based on the average of the values in an array?

From Dev

PHP - Check for duplicate values in an associative array

From Dev

How to combine array values in PHP loop based on the same key

Related Related

  1. 1

    How to remap array keys (based on values) with PHP?

  2. 2

    Remove duplicate values from a multidimensional array in PHP

  3. 3

    Convert numpy.array to order of elements when duplicate values are present

  4. 4

    Java how to sort string array based on order values were placed?

  5. 5

    How to avoid duplicate values in an array of arrays?

  6. 6

    PHP - How to test a multidimensional array for duplicate element values in any order

  7. 7

    Sort array in ascending order and remove duplicate values in objective- c

  8. 8

    How to sort a numpy array into a specific order, specified by a separate list, based on the relative sizes of the values in the array

  9. 9

    PHP multidimensional array, average of duplicate values

  10. 10

    PHP multiple duplicate values in multidimensional array

  11. 11

    In PHP Add the duplicate values in array

  12. 12

    Count of duplicate values in array php

  13. 13

    PHP Search Multidimensional Array for Duplicate Values

  14. 14

    How to order the values a pandas data frame based on the predefined array containing the desired order?

  15. 15

    Remove duplicate values from a multidimensional array in PHP

  16. 16

    Count duplicate values in PHP array

  17. 17

    Java how to sort string array based on order values were placed?

  18. 18

    How can I order an array with duplicate values?

  19. 19

    PHP - How to test a multidimensional array for duplicate element values in any order

  20. 20

    Php merge duplicate array values in a multidimensional array php

  21. 21

    PHP multiple duplicate values in multidimensional array

  22. 22

    In PHP Add the duplicate values in array

  23. 23

    Merge arrays based on duplicate values php

  24. 24

    How to remove duplicate values based on multiple columns

  25. 25

    How to order a php array based on duplicate values

  26. 26

    Custom order for php array based on indexes

  27. 27

    How to sort instances of a class based in ascending order based on the average of the values in an array?

  28. 28

    PHP - Check for duplicate values in an associative array

  29. 29

    How to combine array values in PHP loop based on the same key

HotTag

Archive