Group, assign, and sort values from a multidimensional array in PHP

Asked

Viewed 222 times

0

Hello, everyone. I have the following multidimensional array in PHP:

Array ( 
[0] => Array ( [0] => teste0 [1] => 1 ) 
[1] => Array ( [0] => teste1 [1] => 1 ) 
[2] => Array ( [0] => teste2 [1] => 2 ) 
[3] => Array ( [0] => teste3 [1] => 2 )
[4] => Array ( [0] => teste4 [1] => 2 )
[5] => Array ( [0] => teste5 [1] => 3 )
[6] => Array ( [0] => teste6 [1] => 4 )
[7] => Array ( [0] => teste7 [1] => 4 )
)

I would like to rank this array according to the amount of equal values that are at [1] (numeric). This would produce the following result:

Array ( 
[0] => Array ( [0] => teste2 [1] => 1 ) 
[1] => Array ( [0] => teste3 [1] => 1 ) 
[2] => Array ( [0] => teste4 [1] => 1 ) 
[3] => Array ( [0] => teste0 [1] => 2 )
[4] => Array ( [0] => teste1 [1] => 2 )
[5] => Array ( [0] => teste6 [1] => 4 )
[6] => Array ( [0] => teste7 [1] => 4 )
[7] => Array ( [0] => teste5 [1] => 3 )
)

How to rank using PHP or Javascript?

  • Why did a "teste2" disappear from the first to the second example and appeared a "teste1"?

  • Use the function array_multisort

  • If it is for the amount of equal values, the value should not appear 2 first ? This is the one that has the most "occurrences".

  • You want to order, that’s it?

  • Gives a look here, and see if it helps you. It helped me.

  • In this case, in the first array, since the value 2 is the most repeated, it becomes the new one placed first. The result is its elements receiving the value 1 in the result array.

  • I’ll take a look at this function and the link.

Show 2 more comments

1 answer

1


The easiest way to solve your problem is to create an array with the counts of each element and then sort based on the counts.

For the creation of counts, you can create an empty array and add up each element you pick relative to the position 1 sub-array:

$contagens = Array();
foreach ($arr as $key => $subArr){
    $val = $subArr[1];
    $contagens[$val] = isset($contagens[$val]) ? $contagens[$val] + 1 : 1;
}

That for the array presented in the question gives the following result:

Array
(
    [1] => 2
    [2] => 3
    [3] => 1
    [4] => 2
)

Then you can sort using the function usort, which allows us to pass a comparison function with the logic we want. In your case you can compare by the counts, and when the counts are equal, by the number itself:

usort($arr, function ($a,$b) use ($contagens){
  return $contagens[$a[1]]==$contagens[$b[1]] ? $a[1]-$b[1]: $contagens[$b[1]]-$contagens[$a[1]];
});

Notice I had to use use to make use of the previously found count array.

After this the final array looks like this:

Array
(
    [0] => Array
        (
            [0] => teste4
            [1] => 2
        )

    [1] => Array
        (
            [0] => teste2
            [1] => 2
        )

    [2] => Array
        (
            [0] => teste3
            [1] => 2
        )

    [3] => Array
        (
            [0] => teste0
            [1] => 1
        )

    [4] => Array
        (
            [0] => teste1
            [1] => 1
        )

    [5] => Array
        (
            [0] => teste6
            [1] => 4
        )

    [6] => Array
        (
            [0] => teste7
            [1] => 4
        )

    [7] => Array
        (
            [0] => teste5
            [1] => 3
        )

)

See this example in Ideone

Browser other questions tagged

You are not signed in. Login or sign up in order to post.