4
我試圖自定義PHP的usort
函數來更改字符排序順序。PHP - 更改字符排序順序,例如「a」>「{」= True
目前,只有「*
」符號字符等於小於字母字符的值,例如, 「a
」,即"*" < "a" = TRUE
。其他符號,例如「{
」,具有大於字母的值,例如, 「a
」,即"{" < "a" = FALSE
。
我想排序,因此值「{*}
」位於排序數組的頂部,就好像值爲「*
」。這是我目前使用的函數,通過對象的多個屬性對對象數組進行排序。 [署名:它的Will Shaver's代碼在usort
修改版本docs]
function osort($array, $properties) {
//Cast to an array and specify ascending order for the sort if the properties aren't already
if (!is_array($properties)) $properties = [$properties => true];
//Run the usort, using an anonymous function/closures
usort($array, function($a, $b) use ($properties) {
//Loop through each specified object property to sort by
foreach ($properties as $property => $ascending) {
//If they are the same, continue to the next property
if ($a -> $property != $b -> $property) {
//Ascending order search for match
if ($ascending) {
//if a's property is greater than b, return 1, otherwise -1
return $a -> $property > $b -> $property ? 1 : -1;
}
//Descending order search for match
else {
//if b's property is greater than a's, return 1, otherwise -1
return $b -> $property > $a -> $property ? 1 : -1;
}
}
}
//Default return value (no match found)
return -1;
});
//Return the sorted array
return $array;
}