Usort Without Replacing Keys PHP [duplicate]
Asked Answered
I

3

1

I am having MAJOR usort() issues! :( So basically I want to sort my array by their values. I would like the values to display in this order: Platinum, Gold, Silver, Bronze, Complete, None, Uncomplete. Now I can sort them well, but I would like to preserve their key (is that possible?). here is my code:

function compareMedals( $a, $b ) {
    $aMap = array(1 => 'Platinum', 2 => 'Gold', 3 => 'Silver', 4 => 'Bronze', 5 => 'Complete', 6 => 'None', 7 => 'Uncomplete');
    $aValues = array( 1, 2, 3, 4, 5, 6, 7);
    $a = str_ireplace($aMap, $aValues, $a);
    $b = str_ireplace($aMap, $aValues, $b);
    return $a - $b;
}
usort($list, 'compareMedals');

So is it possible to sort them WHILE preserving their keys? Thank You! :)

EDIT

Array:

$array = array("post1" => 'Platinum', "Post2" => "Bronze, "Post3" = > Gold)

Should output:

"Post1" => 'Platinum',
"Post3" => 'Gold',
"Post2" => 'Bronze'

Yet it is outputting this:

"0" => 'Platinum',
"1" => 'Gold',
"2" => 'Bronze'
Insipid answered 25/3, 2015 at 22:52 Comment(1)
It would be more clear if you described what you get now (and then the difference from the wanted result).Grainfield
F
11

Just use uasort intead of usort. Definition of uasort is

Sort an array with a user-defined comparison function and maintain index association.

There is an example of code with your comparing function and uasort:

$list = array('post1' => 'Gold', 'post2' => 'None', 'post3' => 'Platinum');

function compareMedals( $a, $b ) {
    $aMap = array(1 => 'Platinum', 2 => 'Gold', 3 => 'Silver', 4 => 'Bronze', 5 => 'Complete', 6 => 'None', 7 => 'Uncomplete');
    $aValues = array( 1, 2, 3, 4, 5, 6, 7);
    $a = str_ireplace($aMap, $aValues, $a);
    $b = str_ireplace($aMap, $aValues, $b);
    return $a - $b;
}
uasort($list, 'compareMedals');

print_r($list);

That code gives result

Array
(
    [post3] => Platinum
    [post1] => Gold
    [post2] => None
)
Finegrained answered 25/3, 2015 at 22:56 Comment(4)
How would I go about making that function?Insipid
If I understand your question right you just need to replace usort with uasort. You don't need to change your comparing function or anything else. I have added example of what you get. Do you need this result?Finegrained
It sorts them correctly, but it changes the keys to just the number of its index. So lets say the array was array(post1 => 'Gold', post2 => 'None', post3 => 'Platinum') it then gives me array(2 => Platinum, 0 => Gold, 1 => None). It does not keep the "post" part of the key.Insipid
So uasort does exactly what you need. I updated answer again to make it more clear. But in basic just replace in your code usort($list, 'compareMedals'); with uasort($list, 'compareMedals');Finegrained
C
1

usort did not preserve the key, if you want to preserve key after sort then you should use asort. Hope this help you.

Cell answered 26/3, 2015 at 5:48 Comment(0)
A
0

Considering these assumptions:

  1. Your input array may contain values that are not unique (Gold may be the value for multiple posts).
  2. You know (and can list) all of the potential values which will be used to sort the input array. (there will be no "outliers")

You can simplify your custom sort function by passing your "mapping array" (aka lookup array) as an associative array which has "status" as the keys and "sorting value" as values. I use array_flip() below, but you can write out the 1-dimensional associative array if you wish.

When calling uasort() to call the custom sorting function and preserve the input array's keys, use use() to pass the lookup array inside the function scope.

From php7, you can use the "spaceship operator" (aka three-way-comparison operator) to cleverly perform the comparison.

Clean, Brief, and Efficient.

Code: (Demo)

$array = ["Post1" => "Platinum", "Post2" => "Bronze", "Post3" => "Gold", "Post4" => "Uncomplete", "Post5" => "Gold"];

$order = array_flip(["Platinum", "Gold", "Silver", "Bronze", "Complete", "None", "Uncomplete"]);
// makes: array ('Platinum' => 0, 'Gold' => 1, 'Silver' => 2, 'Bronze' => 3, 'Complete' => 4, 'None' => 5, 'Uncomplete' => 6)

uasort($array, function ($a, $b) use ($order) {
    return $order[$a] <=> $order[$b];
});

var_export($array);

Output:

array (
  'Post1' => 'Platinum',
  'Post3' => 'Gold',
  'Post5' => 'Gold',
  'Post2' => 'Bronze',
  'Post4' => 'Uncomplete',
)

From PHP7.4, you can use arrow syntax to omit the use() declaration and shorten the total syntax.

PHP7.4+ Demo

uasort($array, fn($a, $b) => $order[$a] <=> $order[$b]);
Avast answered 11/10, 2018 at 4:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.