简体   繁体   English

PHP按字母顺序对数组进行排序,除了顶部的一个值,用于下拉菜单

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

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.我试图破解一个下拉菜单,从一系列国家/地区提取信息,以便“美国”(id 1)出现在顶部,而其他所有国家/地区都按字母顺序排序。 How do I sort all EXCEPT United States to remain on top, using usort function for an array?如何使用数组的 usort 函数对所有 EXCEPT United States 进行排序以保持在最前面? 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'); 

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"
}

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 )使用 array_unshift 将资源添加为数组中的第一个元素(array_unshift — 在数组的开头添加一个或多个元素 - 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;
  }
}

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.最简单的方法是摆弄你的 usort 回调的结果。 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.这有点难看,但无需使用其他数组即可完成这项工作,并且一切都在回调中完成,正如预期的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM