There is a simple array on PHP for example:

$array = array("usd","eur","uah","rub"); 

It is necessary to sort it according to my order to get it:

 rub uah usd eur 

Those. if you take the keys then:

 [3,2,0,1] 

I found nothing in Google ...

UPDATED: it turns out you need to complicate the task. It is necessary that the first currency in the list is the one that is set in the variable, for example:

 $defaultCurrency = 'eur'; 

Accordingly, it must be put first, and the rest after it is in order:

 eur rub uah usd 

The default currency can be any

  • one
    1) can be converted to the form [key => value] and then the sorting is not important, since you can choose as you like 2) or use usort , i.e. break through implode by | and compare the keys as you need - BOPOH

4 answers 4

If you need to sort a non-associative array in the order specified in another array, you can make an associative array of the values ​​of the first and keys of the values ​​of the second, sort by keys and pull out the array of values ​​from the result.

Only in the array that sets the order should the numbers go, which you would like to assign to the corresponding elements of the source array. That is, if you want to see the first "rub" in the "ordinal array" should be the smallest number.

 $array = array("usd", "eur", "uah", "rub"); $orderArray = array(2, 3, 1, 0); $tmpArray = array_combine($orderArray, $array); ksort($tmpArray, SORT_NUMERIC); $array = array_values($tmpArray); 

Added by

Regarding the add. here are two solutions: In any case, you need to find the element number with the value of $defaultCurrency .

 $defaultIndex = array_search($defaultCurrency, $array); 

Then the first option:

 if ($defaultIndex !== FALSE) { $orderArray[$defaultIndex] = -1; /* Это чтобы указать что соответствующий элемент должен быть самым первым */ } // Тут предложенная выше сортировка 

The second option:

 if ($defaultIndex !== FALSE) { unset($array[$defaultIndex]); unset($orderArray[$defaultIndex]); } // Тут сортировка if ($defaultIndex !== FALSE) { array_unshift($array, $defaultCurrency); /* Заталкиваем значение по-умолчанию в начало массива */ } 
  • @stckvrw Updated the answer - tutankhamun

For starters, this is not an array, but some kind of nonsense. Array is

 $array = [ "euro" => 2700, "uah" => 2400, "usd" => 100, "rub" => 6600, ]; 

And to sort such a simple array will be the easiest to manually

 $array = [ "rub" => $array['rub'] "euro" => $array['euro'], "uah" => $array['uah'], "usd" => $array['usd'], ]; 
  • And what about the last five lines changed? - tutankhamun
  • Leave, I have updated my question - stckvrw
  • @stckvrw I mean that nothing has changed. Maybe the author of the answer meant something else, but was mistaken when writing the answer - tutankhamun
  • No, I was not mistaken, just at the beginning I had more numbers next to the name of each currency and he thought that the names of the currencies should be the keys of the array, in general it does not matter, forget - stckvrw
  • But why hasn't changed: in the first array, the order is different, where there are currencies with numbers - stckvrw

In general, this is how it should be:

 $array = array("usd","euro","uah","rub"); $sortedArray = array($array[3], $array[2], $array[0], $array[1]); 
  • one
    And you think this is the answer? And why do you convert a simple array into an associative one? If you have a small array, it is enough to do so: $sortedArray = array($array[3], $array[2], $array[0], $array[1]); - tutankhamun
  • Exactly, thanks! Fixed - stckvrw

In pure form, array_multisort :

 $orderArray = array(3, 0, 2, 1); $array = array("usd", "eur", "uah", "rub"); var_dump($orderArray); var_dump($array); array_multisort($orderArray, $array); var_dump($orderArray); var_dump($array); 

It works like this:

 array (size = 4)
   0 => int 3
   1 => int 0
   2 => int 2
   3 => int 1
 array (size = 4)
   0 => string 'usd' (length = 3)
   1 => string 'eur' (length = 3)
   2 => string 'uah' (length = 3)
   3 => string 'rub' (length = 3)
 array (size = 4)
   0 => int 0
   1 => int 1
   2 => int 2
   3 => int 3
 array (size = 4)
   0 => string 'eur' (length = 3)
   1 => string 'rub' (length = 3)
   2 => string 'uah' (length = 3)
   3 => string 'usd' (length = 3)