PHP 5.2 Function needed for GENERIC sorting FOLLOWUP
OK, you guys gave me a great solution for sorting a recordset array last Friday. (PHP 5.2 Function needed for GENERIC sorting of a recordset array)
But now when I implement it, I end up with an extra element in the recordset array. I won't wast space reposting the same info, as the link is above. But the bottom line is that when I sort an array of 5 records, the resulting array has 6 records. The last element in 开发者_开发技巧the array is not a record array, but rather just a element containing an integer value of 1. I presume that it is somehow getting the output value of the "strnatcasecmp" function, but I have no idea how it is happening.
Here is the function that you fine folks provided last week:
function getSortCommand($field, $sortfunc) {
return create_function('$var1, $var2', 'return '.$sortfunc.'($var1["'.$field.'"], $var2["' .$field .'"]);');
}
And here is the line I am calling to sort the array:
$trek[] = usort($trek, getSortCommand('name', 'strnatcasecmp'));
This produces the following output, with an extra element tacked on to the end.
Array
(
[0] => Array
(
[name] => Kirk
[shirt] => Gold
[assign] => Bridge
)
[1] => Array
(
[name] => McCoy
[shirt] => Blue
[assign] => Sick Bay
)
[2] => Array
(
[name] => Scotty
[shirt] => Red
[assign] => Engineering
)
[3] => Array
(
[name] => Spock
[shirt] => Blue
[assign] => Bridge
)
[4] => Array
(
[name] => Uhura
[shirt] => Red
[assign] => Bridge
)
[5] => 1
)
Just do
usort($trek, getSortCommand('name', 'strnatcasecmp'));
usort()
returns a boolean indicating whether it was executed successfully (it sorts the elements in place):
Return Values
Returns TRUE on success or FALSE on failure.
By doing $trek[] = usort(...)
, you append the result of the function to the array.
精彩评论