我有一个数组。例如:
array("Apple", "Orange", "Banana", "Melon");
我想对首先是“橙色”、“甜瓜”的数组进行排序。数组将是
array( "Orange" , "Melon","Apple","Banana");
我查看了 PHP 排序函数,没有找到一个排序函数来完成它。
什么是正确的做法。
谢谢你
最佳答案
您要找的是usort , 可以指定自定义函数对数组进行排序
例子:
function cmp($a, $b)
{
if ($a == "Orange") {
return 1;
}
if ($b == "Orange") {
return -1;
}
return strcmp($a, $b);// or any other sort you want
}
$arr = array("Apple", "Orange", "Banana", "Melon");
usort($arr, "cmp");
关于php - 排序数组 - 该特定值将是第一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25475196/