usort

Why does this usort()-function fail on some versions of PHP?

We wrote some code involving usort which works fine on our development systems (PHP 5.2.8), but are experiencing a problem on our live systems (PHP 5.2.0): // Sort by distance usort($locations, 'Interpolator::sortByDistance'); calls the method (within the same class Interpolator): private static function sortByDistance($a, $b) { ...

PHP's USORT Callback Function Parameters

This is a really esoteric question, but I'm genuinely curious. I'm using usort for the first time today in years, and I'm particularly interested in what exactly is going on. Suppose I've got the following array: $myArray = array(1, 9, 18, 12, 56); I could sort this with usort: usort($myArray, function($a, $b){ if ($a == $b) return...

How to compare 2 strings alphabetically in PHP

What the title says. Specifically if I have $array1['name'] = 'zoo'; $array2['name'] = 'fox'; How can I determine that alphabetically $array2's name should come above $array1's? ...

PHP Sort array by month then year

Hi, I have an array or objects with a dates which I wish to sort by. I have the following custom function that I pass to usort function sortMonths($a, $b) { if ( $a->received_date == $b->received_date ) return 0; return ($a->received_date > $b->received_date) ? 1 : -1; } Which does as required and sorts dates so I get: 2009-...

Sorting multidimensional array in PHP

Hi guys, I am currently creating a sorting method that consists of values from an mysql query. Here's a brief view of the array: Array ( [0] => Array ( ['id'] = 1; ['countries'] = 'EN,CH,SP'; ) [1] => Array ( ['id'] = 2; ...

Using usort in php to sort an array of objects?

I did look at usort, but am still a little confused... Here is what the $myobject object looks like: Array ( [0] => stdClass Object ( [tid] => 13 [vid] => 4 ) [1] => stdClass Object ( [tid] => 10 [vid] => 4 ) [2] => stdClass Object (...

Help with optimising calls to the usort function in PHP

This is my callback for my usort() public function sortProperties($a, $b) { $sortA = inflector::camelize(str_replace('-', '_', $this->sortBy)); $sortB = inflector::camelize(str_replace('-', '_', $this->sortBy)); $a = Arr::get($a, $sortA); $b = Arr::get($b, $sortB); if (is_numeric($a) AND is_nu...

usort - more parameters

Hallo, how can I pass more parameters to usort? I have different functions, which are very similar in structure, I want to have just one function: <?php $arr = array( array('number' => 100, 'string'=>'aaa'), array('number'=>50, 'string'=>'bdef'), array('number'=>150, 'string'=>'cbba') ...

PHP anonymous functions scope question

Hi, I'm trying to sort an array of objects by a common property, however I cannot get my $property parameter to register in the inner function (I can use it in the outer one OK). The way I read the documentation, it sounded like the parameter would be available, have I misunderstood something? Here is what I have: public static fun...

second sorting with php usort

So Ive got a pretty big array of data and need to sort them by two criteria. There is variable $data['important'] and $data['basic']. They are simple numbers and I am using uasort to sort $data firstly by important and then by basic. So Important | Basic 10 | 8 9 | 9 9 | 7 7 | 9 The usort function i...

Sorting an array by a certain key

I have the following array: Array ( [Places] => Array ( [public] => 0 [entities] => Array ( ... ) ) [Issues] => Array ( [public] => 1 [entities] => Array ( ... ...

PHP - usort(): Array was modified by the user comparison function

I have a web application that runs fine on our Linux servers but when running on Mac OS with the Zend Community Edition Server using PHP 5.3 we get the error "usort(): Array was modified by the user comparison function " every time a page loads for the first time (it takes about 2 minutes for a page to tick over and load, on the linux se...

Reorder a PHP array with usort and strcomp: bug

Using usort and strcomp together to order an array by one of its keys has an odd effect: it returns my array with fewer items in it than I put in. The array in my case contains rows representing tasks, and I want to order the rows in the array by the key 'displayorder', which is a number but which I want to be sorted in alphabetical man...

Sort an associative array in php with multiple condition

Consider following array $details = array( array('lname'=>'A', 'fname'=>'P','membkey'=>700,'head'=>'y'), array('lname'=>'B', 'fname'=>'Q','membkey'=>540,'head'=>'n'), array('lname'=>'C', 'fname'=>'R','membkey'=>700,'head'=>'n'), array('lname'=>'D', 'fname'=>'S','membkey'=>540,'head'=>'y'), array('lname'=>'E', 'fname'=>'T','mem...

How do I sort a PHP array by an element nested inside?

I have an array like the following: Array ( [0] => Array ( 'name' => "Friday" 'weight' => 6 ) [1] => Array ( 'name' => "Monday" 'weight' => 2 ) ) I would like to grab the last values in that array (the 'weight'), and use that to sort the main arra...

Using PHP usort with conditional results

Long story short, I need to sort an array of objects using usort, and I need to tell usort which fields in the objects to sort by. The obvious solution is to create tens of separate usort sorting functions, but this seems sort of redundant and ugly. Most of the time, objects will be sorted by input from $_GET, but not always, so I don't...

Question about PHP usort function.

Hi! i've got a PHP script where i rearange a multidimensional array with the use of the usort()-function. this is a sample array (print_r-output) of array $arr Array ( [3] => Array ( [name] => Bjudningen [grade] => 5 [grade_type] => calculated [orgname] => LInvitation ...