all array sorting functions are passed two parameters under the hood; you notice you dont have to specify to walk through the entire array either.
PHP takes care of the arrangement of âwalk the array; for each pair of elements, compare and sort.â your function then must take 2 parameters (the two individual items being sorted at that given moment), and return a value that PHP can use to do the actual sorting.
Note that most comparison functions for sorting return whatâs called a âSpaceship Operator Valueâ - namely, -1,0, or 1:
if -1, A goes before B.
if 0, A and B are of equal value, and are left unsorted relative to each other.
if 1, B goes before A.
You should imagine usort does something like this:
function usort($array, $callback) {
// Get pairs of elements from $array and loop over them
// Actual implementation of createPairsToSort is out of scope
foreach ([$firstElement, $secondElement] = createPairsToSort($array)) {
// Call the user defined function here to see how we should sort
// these two elements
$result = $callback($firstElement, $secondElement);
if ($result < 1) {
// update $array so $firstElement comes before secondElement
} else if ($result === 0) {
// keep elements in order, leave $array alone
} else if ($result > 1) {
// update $array so $firstElement comes after secondElement
}
}
}
[quasi-off-topic]
I know itâs called a spaceship operator for the shape. But iâve always seen it more like a seesaw. The pivotâs at 0, and itâs A (-1) and B (1) sitting on a seesaw, seeing which way it tips.
[/off-topic]
Why do you have $order involved in list_cmp() at all? To my view, the whole point of usort() is that youâve passed $order into it in the first place, so itâs already sorting that array. You seem to be adding another âlayerâ of accessing the array. Youâre also bringing in a global there, which is my pet hate. In your code, if you want to sort a different array but use the same list_cmp, youâll have to have another function to use a different global array. To me, any function should be able to work with the parameters itâs passed in, and not use globals at all.
Iâm going to guess that youâve missed the comma in ($a, $b) as the error is in line 1 and it states that it was expecting either , or ) in the message.
If I paste your code from post #6 (I canât post the latest one because youâve put it as an image, so I presume theyâre the same) and run it in 3v4l.org PHP tester, there are no error messages and it outputs the array in what I presume is the correct order.
In some cases now Iâd be looking for control characters or similar that might have crept in to your file, maybe delete that top line and type it in again and see if that helps.
MS-Word is never a good code editor, just like itâs not a good HTML editor. Iâm using Notepad++ for PHP editing, though there are many more capable things available.
Sorry I am still struggling with usort. After: global $order;
I added following two lines:
echo 'value of dollar_a is $a=' . "$a" . "<BR>"; //modified
echo 'value of dollar_a is $b=' . "$b" . "<BR>"; //modified
I got the following output:
value of dollar_a is $a=2
value of dollar_a is $b=1
value of dollar_a is $a=2
value of dollar_a is $b=3
value of dollar_a is $a=1
value of dollar_a is $b=3
value of dollar_a is $a=2
value of dollar_a is $b=4
value of dollar_a is $a=3
value of dollar_a is $b=4
value of dollar_a is $a=2
value of dollar_a is $b=2
value of dollar_a is $a=2
value of dollar_a is $b=1
value of dollar_a is $a=2
value of dollar_a is $b=1
value of dollar_a is $a=4
value of dollar_a is $b=1
value of dollar_a is $a=3
value of dollar_a is $b=1
value of dollar_a is $a=1
value of dollar_a is $b=1
value of dollar_a is $a=2
value of dollar_a is $b=2
It looks that $a & $b values correspond to the $array values. Initially $a=2 and $b = 1, i.e., first two values of $array. $Key i.e. index of $order is 0, hence, $value is 1. Now in the for loop we have :
if(2 == 1)
which is false, so we would return 0, where we would go? How the loop is continuing after return?
Somebody please guide me.
Zulfi.
Surely, it isnât continuing? Itâs just being called repeatedly by usort with different parameters. Why not add a few more echo statements so you can follow exactly what is happening? Add one after the end of the loop, and see if it displays, for example.