0

How can I sort an array based on another array? I'm speaking about the values, not the keys. I've found plenty of questions about how to sort the keys but none about the values.

Imagine I have this array :

$sizes = array("L", "XXXL", "M", "S");

How can I reorder these to be like this :

$newsizes = array("S", "M", "L", "XXXL");

based on a fixed human logical array like this one :

$sizeincrement = array("XS", "S", "M", "L", "XL", "XXL", "XXXL");

And considering elements that are not in the sizeincrement array should be appended at the end.

I thought this would exists as a ready-made sort function, but it appears it's not the case: PHP SORTING

4
  • array_intersect() will do the sort you need without resorting to custom functions or usort. See my answer. Commented Jan 8, 2014 at 19:11
  • @DigitalChris Though it's debatable whether intersecting, then diffing, then merging arrays is really a more efficient approach than usort. =) Commented Jan 8, 2014 at 19:13
  • @jszobody no, I think you missed the point. I ALSO answered his second part about "lements that are not in the sizeincrement array should be appended at the end." The single line array_intersect accomplished what the other answers did. Commented Jan 8, 2014 at 19:14
  • Indeed the only wrong part is that elements not being present are prepended not appended 3v4l.org/C2LtC Commented Jan 8, 2014 at 19:17

3 Answers 3

1

Use of common array functions can accomplish this:

<?php
$sizes = array("L", "XXXL", "M", "S");

$sizeincrement = array("XS", "S", "M", "L", "XL", "XXL", "XXXL");

//this will give you the array you need in the order you need:
$sorted_array = array_intersect($sizeincrement,$sizes);

//now get the ones NOT in the array:

$missing_array = array_diff($sizeincrement,$sizes);

$final_array = array_merge($sorted_array, $missing_array);

var_dump($final_array);
?>

Here it is working: https://eval.in/87279

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

1 Comment

Thanks Chris, this is the solution considering the last part of my question
1

A custom function can do the job really quickly ! here is my guest

$sizes = array("L", "XXXL", "M", "S");

function sort_size($arr) {
    $out = array();

    foreach (array("XS", "S", "M", "L", "XL", "XXL", "XXXL") as $size) {
        if (in_array($size, $arr)) {
            $out[] = $size;
        }
    }

    return $out;
}

$newSizes = sort_size($sizes);

like that you can add or remove new sizes directly in the array in the foreach.

hope that help!

2 Comments

I think your answer is missing the case of some values not present in the reference array, They will be lost. I could be wrong
This gives the sorted $sizes, but doesn't append the missing elements to the end.
1

You could do this with a custom usort callback function pretty easily. Something like this:

function compare($a, $b) {
    $sizeincrement = array("XS", "S", "M", "L", "XL", "XXL", "XXXL");
    if(!array_search($a, $sizeincrement)) return 1;
    if(!array_search($b, $sizeincrement)) return -1;
    return array_search($a, $sizeincrement) > array_search($b, $sizeincrement);
}

Then call:

usort($sizes, "compare");

Working example: http://3v4l.org/2Hp0g

4 Comments

You forgot to add the ones NOT in the initial array at the end.
@VincentDuprez no look at his output: it has only 4 elements.
Upvoted your answer as it pointed me to the right path thanks, I will set Digital chris answer as correct answer as it is considering the missing elements..
@VincentDuprez Had to step away for a sec, sorry. Glad to help at least show you the right path, I'd strongly encourage sorting functions for this. Updated my answer, it's trivial to add more logic if needed for things like extra elements (or anything else you might need to take into account).

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.