並べ替える必要がある配列があります。国コードの配列です。
$countries = array('uk', 'fr', 'es', 'de', 'it');
最初に特定のユーザーが選択した国で配列をソートする必要があります。'fr' と残りの項目はアルファベット順に並べる必要があります。
これを行う方法がよくわかりません。助けていただければ幸いです。
$countries = array('uk', 'fr', 'es', 'de', 'it');
// find and remove user value
$uservar = 'uk';
$userkey = array_search($uservar, $countries);
unset($countries[$userkey]);
// sort ascending
sort($countries,SORT_ASC);
// preappend user value
array_unshift($countries, $uservar);
これは少し長いですが、動作するはずです。
<?php
$user_selected = 'fr';
$countries = array('uk', 'fr', 'es', 'de', 'it');
unset($countries[ array_search($user_selected, $countries) ]); // remove user selected from the list
sort($countries); // sort the rest
array_unshift($countries, $user_selected); // put the user selected at the beginning
print_r($countries);
?>
// The option the user selected
$userSelectedOption = 'fr';
// Remove the user selected option from the array
array_splice($countryCodes, array_search($userSelectedOption, $countryCodes), 1);
// Sort the remaining items
sort($countryCodes, SORT_ASC);
// Add the user selected option back to the beginning
array_unshift($countryCodes, $userSelectedOption);