-1

I have one array:

 $array_sorter = [  
                    'XXS' => 1,   
                    'XS' => 2,  
                    'S' => 3,   
                    'M' => 4,   
                    'L' => 5,   
                    'XL' => 6,   
                    'XXL' => 7  
                 ];

and another one that could be like this:

 $array_to_sort = ('XS','M','XL','S','L','XXS')

how can I do to sort the second one based upon the first one?

I mean:

$array_to_sort are sizes but they are random inside this array

I need to print the list of available sizes ($array_to_sort) but in the order of $array_sorter

  • What you have tried so for and what is your expected outcome? – Alive to die - Anant Aug 23 '16 at 08:31
  • What do you mean 'based on the first one'? – RGriffiths Aug 23 '16 at 08:32
  • How is it supposed to be sorted? Your example is neither alphabetical nor numerical by value of the first array. It just looks like a random order. Please be more specific in what you're expecting. – Oldskool Aug 23 '16 at 08:35
  • http://php.net/usort with a closure `use ($array_sorter)` (or hard-coded), also probably duplicate – Jakumi Aug 23 '16 at 08:40
  • Foreach the first array: `foreach($array_sorter as $key => $value ) { }` and use the key only to write another array, so you dont have to use this other $array_to_sort at all... – Denis Solakovic Aug 23 '16 at 09:15

3 Answers3

0

As stated by @Jakumi, usort would be the way to go. However this way might be easier to understand if you are a beginner :

$array_sorter = [
    'XXS' => 1,
    'XS' => 2,
    'S' => 3,
    'M' => 4,
    'L' => 5,
    'XL' => 6,
    'XXL' => 7,
];

$array_to_sort = array('XS', 'M', 'XL', 'S', 'L', 'XXS');

$array_sorted = array();

foreach($array_to_sort as $item){
    $k = $array_sorter[$item];
    $array_sorted[$k] = $item;
}
ksort($array_sorted);
vincenth
  • 1,732
  • 5
  • 19
  • 27
0

Simply use the first array as a "weight"-provider. I mean the first array have a weight for every entry of your second array (e.g. M get the weight 4).

loop over the second array and create a thrid one: $weightArray like this:

$weightArray = array();
foreach($array_to_sort as $value){
  $weight = $array_sorter[$value];
  if(!isset($weightArray[$weight])){
    $weightArray[$weight] = array();
  }
  $weightArray[$weight][] = $value;
}

Now you have an array like this: (2=>'XS', 4=>'M', 5=>'XL', 3=>'S', 5=>'L', 1=>'XXS')

now you can sort it by key ksort() and copy it back to your source array like this:

$array_to_sort = array_values(ksort($weightArray));

PS: if you have something like $array_to_sort = ('XS','M','M','M','L','XXS') it will also work => $array_to_sort = ('XXS','XS','M','M','M','L')

Marcus
  • 1,910
  • 2
  • 16
  • 27
0

Thanks to all for you suggestion

I found alone this solution:

my mistake was focusing on the array to be sorted ($arrat_to_sort)

instead I turned the problem:

as $array_sorter always contains all possible values of $arrat_to_sort in the right order, I used the function array_intersect($ array_sorter, $ arrat_to_sort)

and immediately I have an array with the values of $ arrat_to_sort in the $array_sorter position