Questions tagged [usort]

a function in PHP which provides means for custom collections sorting by using an user-defined comparison function to determine the order of elements

bool usort ( array &$array , callable $cmp_function )

This function will sort an array by its values using a user-supplied comparison function. If the array you wish to sort needs to be sorted by some non-trivial criteria, you should use this function.

PHP documentation for this function can be found here

382 questions
47
votes
4 answers

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…
Michael
  • 481
  • 1
  • 5
  • 5
43
votes
5 answers

Pass extra parameters to usort callback

I have the following functions. WordPress functions, but this is really a PHP question. They sort my $term objects according to the artist_lastname property in each object's metadata. I want to pass a string into $meta in the first function. This…
djb
  • 4,694
  • 4
  • 34
  • 45
41
votes
8 answers

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…
geerlingguy
  • 4,252
  • 6
  • 50
  • 85
37
votes
1 answer

Keeping array index key when sorting a multidimensional array with PHP

array(10) { [1019]=> array(3) { ["quantity"]=> int(0) ["revenue"]=> int(0) ["seller"]=> string(5) "Lenny" } [1018]=> array(3) { ["quantity"]=> int(5) ["revenue"]=> int(121) ["seller"]=> string(5) "Lenny" } [1017]=> array(3) { ["quantity"]=>…
Karem
  • 16,343
  • 69
  • 163
  • 271
26
votes
5 answers

In php how does usort() function works

I have looked at the php documentation, tutorials online and none of them how usort is actually working. I have an example i was playing with below. $data = array( array('msg' => 'some text','month' => 11,'level' => 10), array('msg' =>…
Exploit
  • 5,604
  • 16
  • 63
  • 93
22
votes
5 answers

usort descending

When i try to apply the below code from here usort($myArray, function($a, $b) { return $a['order'] - $b['order']; }); it gives me results in ascending order. Output: 0 0 0 0 0 0.29 1.09 6.33 On swapping $a and $b it gives the results in…
000
  • 3,818
  • 4
  • 21
  • 38
17
votes
3 answers

PHP usort won't sort class

This is a sample of the array of elemnts to sort: $items = array 0 => object(stdClass)[8] public 'id' => string '110' (length=3) public 'brand_id' => string '18' (length=2) array 0 =>…
Nacho
  • 7,544
  • 15
  • 57
  • 74
11
votes
3 answers

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,…
Sampson
  • 251,934
  • 70
  • 517
  • 549
10
votes
2 answers

Access array key using uasort in PHP

If have a rather basic uasort function in PHP that looks like this: uasort($arr, function($a, $b) { if ($a > $b) return -1; if ($a < $b) return 1; ... } The array I'm trying to…
mmvsbg
  • 3,362
  • 17
  • 46
  • 67
8
votes
5 answers

Sort Multi-dimensional array by decimal values

What I'm trying to do is sort a multi-dimensional array that contains decimal values. From what I've tested, floats are having trouble being ordered properly. Array ( [0] => Array ( [company] => Ebay [weight] =>…
stwhite
  • 2,516
  • 4
  • 30
  • 58
7
votes
2 answers

PHP additional parameters to usort

The following code lays within a function which itself lays within a class. Its purpose is to avoid having one sorting function per $filter value : $GLOBAL['filter'] = $filter; usort($this->data, function($arr1, $arr2) { return…
Lanz
  • 107
  • 1
  • 6
7
votes
2 answers

Possible to pass a closure to usort in PHP?

I have an array sorting function as follows: public function sortAscending($accounts) { function ascending($accountA, $accountB) { if ($accountA['AmountUntilNextTarget'] == $accountB['AmountUntilNextTarget']) { return 0; …
igniteflow
  • 7,356
  • 9
  • 35
  • 44
7
votes
2 answers

PHP -- usort is modifying contents of objects in array, how do I prevent this?

I am using usort with a user comparison function to sort an array of objects. After running usort on an array of these objects, I've found that some of the values of the objects have changed along with their position in the array. What am I…
Macy Abbey
  • 3,719
  • 1
  • 18
  • 30
7
votes
3 answers

PHP usort() expects parameter 2 to be a valid callback, not in a class

I have a problem with usort not liking the second parameter (the sorting function). I have seen a lot of questions about this being a problem when in a class, and the answer for that is to pass an array of array($this, functionName) But in my case…
Deltaxfx
  • 303
  • 2
  • 10
7
votes
2 answers

PHP usort reorders array the sort value is the same for all

I'm using usort to sort an array with an associative array within each element. When all of the values I am sorting on in the array are the same then it still changes the position of the elements in the array, is there a way to prevent this? For…
Chris
  • 2,705
  • 4
  • 31
  • 51
1
2 3
25 26