PHP 5.2 Function needed for GENERIC sorting FOLLOWUP

Posted by donbriggs on Stack Overflow See other posts from Stack Overflow or by donbriggs
Published on 2010-05-24T19:38:37Z Indexed on 2010/05/24 19:41 UTC
Read the original article Hit count: 290

OK, you guys gave me a great solution for sorting a recordset array last Friday. (http://stackoverflow.com/questions/2884325/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

)

© Stack Overflow or respective owner

Related posts about php

Related posts about sort