PHP Sort an array alphabetically except ONE value on top, for a dropdown menu

对着背影说爱祢 提交于 2019-12-06 11:58:20

问题


I am trying to hack a dropdown menu pulling info from an array of countries so that 'United States' (id 1) appears on the top, while every other country is sorted by alphabetical order. How do I sort all EXCEPT United States to remain on top, using usort function for an array? Any alternative suggestions are also welcome. Here is the code:

while (list($key, $value) = each($countries->countries)) {
   $countries_array[] = array('id' => $key, 'text' => $value['countryname']);
}
function text_cmp($a, $b) {
   return strcmp($a["text"], $b["text"]);
} 
usort($countries_array, 'text_cmp'); 

回答1:


The easiest way to do this is to remove the single value, sort and then re-add it:

working example

// your countries array goes here:
$countries = array(2=>"Burma", 4=>"Zimbabwe", 10=>"France", 1=>"United States", 13=>"Russia");

$AmericaFKYeah = array(1=>"United States");
$countries =array_diff($countries, $AmericaFKYeah);
asort($countries);

// using + instead of array_unshift() preserves numerical keys!
$countries= $AmericaFKYeah + $countries;

gives you:

array(5) {
  [1]=>
  string(13) "United States"
  [2]=>
  string(5) "Burma"
  [10]=>
  string(6) "France"
  [13]=>
  string(6) "Russia"
  [4]=>
  string(8) "Zimbabwe"
}



回答2:


With Indexed Array

Steps

  1. Find resource in array
  2. Unset variable in array
  3. Add resource as the first element in array with array_unshift (array_unshift — Prepend one or more elements to the beginning of an array - http://php.net/manual/en/function.array-unshift.php)

Code

 if (in_array('yourResource', $a_array)){
    unset($a_array[array_search('yourResource',$a_array)]);
    array_unshift($a_array, 'yourResource');
 }

With MultiDimensional Arrays

$yourResorceToFind = 'yourResource';
foreach ($a_arrays as $a_sub_array){
  if (in_array($yourResorceToFind, $a_sub_array)){

    unset($a_arrays[array_search($yourResorceToFind,$a_sub_array)]);
    array_unshift($a_arrays, $a_sub_array);
    break;
  }
}



回答3:


I found this topic trying to do the same thing in a ugly way but since my technique seems simpler I share it if someone needs.

The easiest way of doing it is by fiddling with the result of your usort callback. You just have to pass the value you want first in your callback like this :

$countries = [
 ['id' => 2, 'text' => 'Burma'],
 ['id' => 4, 'text' => 'Zimbabwe'],
 ['id' => 10, 'text' => 'France'],
 ['id' => 1, 'text' => 'United States'],
 ['id' => 13, 'text' => 'Russia'],
];

$myFirstItem = "United States";

usort($countries, function($a, $b) use ($myFirstItem) {
    return $myFirstItem == $a['text'] ? -1 : ($myFirstItem == $b['text'] ? 1 : strcmp($a['text'], $b['text']));
});

It's kind of ugly but does the job without the need to play with other arrays and everything is done in the callback, as expected.



来源:https://stackoverflow.com/questions/22791371/php-sort-an-array-alphabetically-except-one-value-on-top-for-a-dropdown-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!