PHP usort Understanding

84 views Asked by At
function cmp_function($a, $b)

{

if ($a == $b) return 0;
  return ($a > $b) ? -1 : 1;

}

$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana" );

usort($fruits, "cmp_function");

Question: What values will be passed for $a and $b parameters for cmp_fucntion()?

1

There are 1 answers

0
Justinas On BEST ANSWER

$a will be first element of two compared at this point, while $b will be second compared element at this point of sorting algorithm.

E.g.:

 iteration |     $a   |    $b
 -------------------------------
      1      "lemon"  |   "orange"
      2      "orange" |   "banana"
      3      "banana" |   "lemon"
           ...