0

I'm having trouble getting usort to work and not sure what I'm missing. Below is an example of my array. I want to sort the array based on the value of the sort key.

Array
(
[0] => Array
    (
        [sort] => 1520546956
        [row] => Data lives here
    )

[1] => Array
    (
        [sort] => 1521047928
        [row] => Data lives here
    )

[2] => Array
    (
        [sort] => 1520525366
        [row] => Data lives here
    )

[3] => Array
    (
        [sort] => 1520525227
        [row] => Data lives here
    )

My code to try and sort this is:

foreach ($resultsArray as $record)
{
    usort($record['sort'], function($a, $b)
        {
            if ($a == $b)
                {
                    return 0;
                }
            return ($a < $b) ? -1 : 1;  
        });
}

However my code seems to be ineffective as the order of the array isn't changing. I feel like I'm close but can't identify what I'm missing. Thank you for any help!

3
  • $record will be a copy of the array element, not a reference. Thus, any changes you make to the element will not persist. To fix this, either do foreach($resultsArray as $index=>$record) and then $resultsArray[$index] = $record after sorting, or do foreach($resultsArray as &$record)--note the & in this second example! Commented Mar 20, 2018 at 23:49
  • 1
    Additionally, your sort logic itself is off. usort sorts the given array in place. Thus, $record['sort'] should be an array. If you want to use usort properly, do usort($resultsArray, function($a, $b) { /* compare $a['sort'] and $b['sort'] */ }! This will completely avoid the need for a foreach loop. Commented Mar 20, 2018 at 23:52
  • @B.Fleming That did the trick - ending up going with usort($resultsArray, function($a, $b) { /* compare $a['sort'] and $b['sort'] */ } and working correctly now. Thank you! Commented Mar 20, 2018 at 23:56

4 Answers 4

1

A different approach to accomplish the same functionality is to use array_multisort with the desired combination of sorting flags.

Dataset:

$resultsArray = array(
  array('sort'=>1520546956, 'row'=>'row 0 data'),
  array('sort'=>1521047928, 'row'=>'row 1 data'),
  array('sort'=>1520525366, 'row'=>'row 2 data'),
  array('sort'=>1520525227, 'row'=>'row 3 data')
);

array_multisort Example:

$sortValues = array_column($resultsArray, 'sort');
array_multisort($sortValues, SORT_ASC, $resultsArray);
print_r($resultsArray);

Results: https://3v4l.org/NpVIc

Array
(
    [0] => Array
        (
            [sort] => 1520525227
            [row] => row 3 data
        )

    [1] => Array
        (
            [sort] => 1520525366
            [row] => row 2 data
        )

    [2] => Array
        (
            [sort] => 1520546956
            [row] => row 0 data
        )

    [3] => Array
        (
            [sort] => 1521047928
            [row] => row 1 data
        )

)

Alternatively you can still use usort, but in your function, you need to retrieve the associated array key named sort in order to compare the values.

usort Example:

usort($resultsArray, function($a, $b) { 
    if ($a['sort'] == $b['sort']) {
        return 0;
    }
    return ($a['sort'] < $b['sort'] ? -1 : 1);
});
print_r($resultsArray);

Results: https://3v4l.org/5nfbc

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

Comments

0

Modified code to reflect the below as suggested:

usort($resultsArray, function($a, $b) { /* compare $a['sort'] and $b['sort'] */ }

Working perfectly.

Comments

0

$record['sort'] would have to be an array for that to work. Yet still, it does nothing.

I'm pretty sure you want to do this:

<?php
$multiArray = array(
  array('sort'=>1520546956, 'row'=>'row 0 data'),
  array('sort'=>1521047928, 'row'=>'row 1 data'),
  array('sort'=>1520525366, 'row'=>'row 2 data'),
  array('sort'=>1520525227, 'row'=>'row 3 data'));
foreach($multiArray as $a){
  $numArray[] = $a['sort'];
}
asort($numArray, SORT_NUMERIC);
foreach($numArray as $k => $v){
  $resArray[] = $multiArray[$k];
}
print_r($resArray);
?>

Comments

0

It is because php foreach construct works on the copy of the array provided(ie: $resultsArray), where php usort() function references or points to the same array. That is why your code is not working as expected. If you don't understand this concept I suggest you a good online course by Kevin skoglund (php essential training) in Lynda.com

2 Comments

@Oighea I have not asked any question but have answered it. clarify what you are trying to say.
It isn't grammatically correct. Proper grammar is required since this isn't a chat room nor an informal discussion board.

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.