Custom order elements by Value into an array

Viewed 42
Array (
[1090] => BENNETON
[6761] => BENNETON
[3963] => BENNETON
[1662] => BENNETON
[1099] => BENNETON
[694] => XELOLE
[2527] => XELOLE
[4553] => XELOLE
[7908] => XELOLE
[4787] => XELOLE
[8261] => GOLDEN
[8259] => GOLDEN
[8265] => GOLDEN
[2599] => VICTORYE
[860] => VICTORYE
[1117] => VICTORYE
[844] => VICTORYE
[2243] => MIMIC
[1035] => MIMIC
[1025] => MIMIC
[561] => MIMIC
[621] => MIMIC
[2404] => OPTIMUM 
[2389] => OPTIMUM 
[2396] => OPTIMUM 
[1965] => OPTIMUM 
[2403] => OPTIMUM 

How can i make all elements with Value MIMIC (and key), be the first in the array, followed by all with GOLDEN, then all the other elements ?

2 Answers

You can use uasort to implement a custom sorting function. Something like this would work:

//$sortMe = your array
uasort($sortMe, function($a, $b){
    if ($a === $b) { //if both values are the same
        return 0; //don't change their position
    }

    if ($a === 'MIMIC' || $b === 'MIMIC') { //If either a or b are MIMIC (a and b can't both be MIMIC since that would have met our previous return condition
         return $a === 'MIMIC' ? 1:-1; //If a is "MIMIC" it should be first, otherwise put b in front of a
    }

    if ($b === 'GOLDEN' || $b === 'GOLDEN') { //Do the same thing with GOLDEN (note, if either variable was "MIMIC" we would have already exited the function at this point
        return $a === 'GOLDEN' ? 1:-1;
    }

    return 0; //Neither of our sort-words were found, don't change the order.
});
var_export($sortMe); //Will be sorted now.

From the manual array operators:

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

So you simply filter out the goldens and the mimics, and then form the union of the filtered arrays and the original array.

<?php
$items =[
    '1090' => 'BENNETON',
    '6761' => 'BENNETON',
    '3963' => 'BENNETON',
    '1662' => 'BENNETON',
    '1099' => 'BENNETON',
    '694' => 'XELOLE',
    '2527' => 'XELOLE',
    '4553' => 'XELOLE',
    '7908' => 'XELOLE',
    '4787' => 'XELOLE',
    '8261' => 'GOLDEN',
    '8259' => 'GOLDEN',
    '8265' => 'GOLDEN',
    '2599' => 'VICTORYE',
    '860' => 'VICTORYE',
    '1117' => 'VICTORYE',
    '844' => 'VICTORYE',
    '2243' => 'MIMIC',
    '1035' => 'MIMIC',
    '1025' => 'MIMIC',
    '561' => 'MIMIC',
    '621' => 'MIMIC',
    '2404' => 'OPTIMUM',
    '2389' => 'OPTIMUM',
    '2396' => 'OPTIMUM',
    '1965' => 'OPTIMUM',
    '2403' => 'OPTIMUM'
];

$goldens = array_filter($items, function($v) { return $v == 'GOLDEN';});
$mimics  = array_filter($items, function($v) { return $v == 'MIMIC';});

$result = $goldens + $mimics + $items;

var_export($result);

Output:

array (
  8261 => 'GOLDEN',
  8259 => 'GOLDEN',
  8265 => 'GOLDEN',
  2243 => 'MIMIC',
  1035 => 'MIMIC',
  1025 => 'MIMIC',
  561 => 'MIMIC',
  621 => 'MIMIC',
  1090 => 'BENNETON',
  6761 => 'BENNETON',
  3963 => 'BENNETON',
  1662 => 'BENNETON',
  1099 => 'BENNETON',
  694 => 'XELOLE',
  2527 => 'XELOLE',
  4553 => 'XELOLE',
  7908 => 'XELOLE',
  4787 => 'XELOLE',
  2599 => 'VICTORYE',
  860 => 'VICTORYE',
  1117 => 'VICTORYE',
  844 => 'VICTORYE',
  2404 => 'OPTIMUM',
  2389 => 'OPTIMUM',
  2396 => 'OPTIMUM',
  1965 => 'OPTIMUM',
  2403 => 'OPTIMUM',
)
Related