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); /* Заталкиваем значение по-умолчанию в начало массива */ }
[key => value]and then the sorting is not important, since you can choose as you like 2) or useusort, i.e. break through implode by | and compare the keys as you need - BOPOH