nomadsoul Posted May 29, 2011 Share Posted May 29, 2011 I'm having trouble understanding how the array is being sorted. Specifically; each echo statement is the same so how does it know which function to echo? Thanks $students = array ( 256 => array ('name' => 'Jon', 'grade' => 98.5), 2 => array ('name' => 'Vance', 'grade' => 85.1), 9 => array ('name' => 'Stephen', 'grade' => 94.0), 364 => array ('name' => 'Steve', 'grade' => 85.1), 68 => array ('name' => 'Rob', 'grade' => 74.6) ); // Name sorting function: function name_sort ($x, $y) { return strcasecmp($x['name'], $y['name']); } // Grade sorting function: // Sort in DESCENDING order! function grade_sort ($x, $y) { return ($x['grade'] < $y['grade']); } // Print the array as is: echo '<h3>Array As Is</h3><pre>' . print_r($students, 1) . '</pre>'; // Sort by name: uasort ($students, 'name_sort'); // Print the array now: echo '<h3>Array Sorted By Name</h3><pre>' . print_r($students, 1) . '</pre>'; // Sort by grade: uasort ($students, 'grade_sort'); */ // Print the array now: echo '<h3>Array Sorted By Grade</h3><pre>' . print_r($students, 1) . '</pre>'; ?> </body> </html> Link to comment Share on other sites More sharing options...
Antonio Conte Posted May 30, 2011 Share Posted May 30, 2011 It's because of the sorting before the echo statements. // NORMAL SORTING // Print the array as is: echo '<h3>Array As Is</h3><pre>' . print_r($students, 1) . '</pre>'; // Sort by name: uasort ($students, 'name_sort'); // Print the array now: echo '<h3>Array Sorted By Name</h3><pre>' . print_r($students, 1) . '</pre>'; // Sort by grade: uasort ($students, 'grade_sort'); // Print the array now: echo '<h3>Array Sorted By Grade</h3><pre>' . print_r($students, 1) . '</pre>'; 1 Link to comment Share on other sites More sharing options...
nomadsoul Posted May 30, 2011 Author Share Posted May 30, 2011 Thanks for your reply. Do you mean that if I change the order (put the echo before the function) it won't work? I've always had trouble understanding the syntax of functions. It's because of the sorting before the echo statements. // NORMAL SORTING // Print the array as is: echo '<h3>Array As Is</h3><pre>' . print_r($students, 1) . '</pre>'; // Sort by name: uasort ($students, 'name_sort'); // Print the array now: echo '<h3>Array Sorted By Name</h3><pre>' . print_r($students, 1) . '</pre>'; // Sort by grade: uasort ($students, 'grade_sort'); // Print the array now: echo '<h3>Array Sorted By Grade</h3><pre>' . print_r($students, 1) . '</pre>'; Link to comment Share on other sites More sharing options...
Antonio Conte Posted May 31, 2011 Share Posted May 31, 2011 I had that too. It's actually very simple. // A normal variable containing a number $a-number = 10; // This is 10 because it's before we use the function echo $a-number; // Now we use our function (see bottom) on this variable add_ten($a-number); // This is now 20 because of the function below echo $a-number; // This is a function that adds ten to the value function add_ten($function-variable) { return $function-variable + 10; } It does not matter what variable you put into the "use" part of the function. It can be named anything. That is why it's great. You could have a variable called "$another-number" and add ten to that as well. 1 Link to comment Share on other sites More sharing options...
nomadsoul Posted June 1, 2011 Author Share Posted June 1, 2011 Thanks, that helps. Link to comment Share on other sites More sharing options...
bahaa Posted June 5, 2011 Share Posted June 5, 2011 Can any one explain to me what the $x and $y refer to in the sorting function ? Link to comment Share on other sites More sharing options...
Josee Posted June 5, 2011 Share Posted June 5, 2011 Think of it as meaning "one element from the $students array that we shall call $x (or anything else)" and "another element from the $students array that we shall call $y (or anything else)". So the function will take for instance $students[9][name] (value: "Stephen") and compare it with $students[2][name] (value: "Vance"); and then go on comparing pairs of elements from the array to decide which must come first. 1 Link to comment Share on other sites More sharing options...
bahaa Posted June 5, 2011 Share Posted June 5, 2011 Thanks Josee I understand it now. Link to comment Share on other sites More sharing options...
Recommended Posts