0

I have an array in PHP which has associative index values as show below. They are irrelevant to the sorting that I need to do, but I need to keep them. Each value in this array is actually another array which might have 1 to N integers in it, sorted in descending order. However, the integers are written down as text (strings). So the initial data looks something like that:

[10025] => 51, 51, 43, 43, 30, 29, 28, 27, 25, 24, 22, 21
[15671] => 24, 21
[02672] => 24, 26
[76935] => 87, 72, 69, 67, 55, 43, 43, 40, 35, 33, 29

I need to sort this by looking at the maximum value of each secondary array. If the values are equal, I have to look at the second value, etc so finally I'll get something like this:

[76935] => 87, 72, 69, 67, 55, 43, 43, 40, 35, 33, 29
[10025] => 51, 51, 43, 43, 30, 29, 28, 27, 25, 24, 22, 21
[02672] => 24, 26
[15671] => 24, 21

arsort() by default looks at the number of items in each array, which doesn't do the job and raising the SORT_NUMERIC doesn't work either - can't figure out what it does exactly but it doesn't sort the array the way I want.

I've also looked at array_multisort(), however, playing around with it, I couldn't get the desired result either.

3 Answers 3

2

Try this, take note of the uasort() function (that's where the magic happens):

 <?php
$data = array(
    10025 => '51, 51, 43, 43, 30, 29, 28, 27, 25, 24',
    15671 => '24, 21',
    02672 => '24, 26',
    76935 => '87, 72, 69, 67, 55, 43, 43, 40, 35, 33, 29'
); //Inital data array

echo '<pre>'.print_r($data, true).'</pre>'; //Print the array as it is

uasort($data, function($a, $b) { //Sort the array using a user defined function maintaining array indices
    $aArray = explode(', ', $a); //Split the strings into proper arrays
    $bArray = explode(', ', $b);

    rsort($aArray); //Sort the proper arrays from largest to smallest
    rsort($bArray);

    return $aArray[0] > $bArray[0] ? -1 : 1; //Compare the first element in each array (because it is the largest thanks to the rsort functions)
});

echo '<pre>'.print_r($data, true).'</pre>'; //Print the array in it's new order
?>

Output:

Array
(
    [10025] => 51, 51, 43, 43, 30, 29, 28, 27, 25, 24
    [15671] => 24, 21
    [1466] => 24, 26
    [76935] => 87, 72, 69, 67, 55, 43, 43, 40, 35, 33, 29
)
Array
(
    [76935] => 87, 72, 69, 67, 55, 43, 43, 40, 35, 33, 29
    [10025] => 51, 51, 43, 43, 30, 29, 28, 27, 25, 24
    [1466] => 24, 26
    [15671] => 24, 21
)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, that's a good tip. I have a question though. You stated $aArray[0] > $bArray[0] etc. If they are equal, does it automatically go to check the first index, then the second and so on?
Not as it currently stands, it just looks for the first number, it wouldn't be too hard to change it though, a simple loop would do it.
Yes, a little more research showed exactly the same thing. Thank you!
0

array_multisort() should do the trick:

$yourdata; // your assosiative array with data

$lengths = array();
foreach ($yourdata as $key => $row)
{
    $lengths[$key] = $row.length;
}
array_multisort($lengths, SORT_DESC, $yourdata);

Reference url http://www.php.net/manual/ro/function.array-multisort.php

Comments

0
uasort(
    $myArray,
    function($a, $b) {
        $a = explode(',',$a);
        $b = explode(',',$a);
        $i = 0;
        while (true) {
            if (isset($a[$i]) && isset($b[$i]) && $a[$i] !== $b[0]) {
                return ($a[$i] < $b[$i]) ? -1 : 1;
            } elseif (isset($a[$i]) && !isset($b[$i])) {
                return 1;
            } elseif (isset($b[$i]) && !isset($a[$i])) {
                return -1;
            } elseif (!isset($b[$i]) && !isset($a[$i])) {
                return 0;
            }
            $i++;
        }
    }
);

Comments

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.