Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I sort this array by key with usort?

I think I might have read every usort article on StackOverflow, but I can't work out this one. It might be that usort is not the tool I need? Here's a bit of the array that I'm working with (I have it assigned to $allPages):

Array
(
    [0] => Page Object
        (
            [id] => 4
            [slug] => articles
            [created_on] => 2009-08-06 07:16:00
        )

    [1] => Page Object
        (
            [id] => 99
            [slug] => a-brief-history
            [created_on] => 2011-04-25 12:07:26
        )

    [2] => Page Object
        (
            [id] => 98
            [slug] => we-arrive
            [created_on] => 2011-04-24 13:52:35
        )

    [3] => Page Object
        (
            [id] => 83
            [slug] => new-year
            [created_on] => 2011-01-02 14:05:12
        )
)

I am trying ultimately to sort on the created_on value, but for the moment, I'd settle on being able to sort on any of them! When I try the normal cmp($a, $b) type callback with usort -- as, for example, in this answer on a usort question -- I just get a blank. Example:

function cmp($a, $b) {
  return strcmp($a["slug"], $b["slug"]);
}
usort($allPages, 'cmp')

And print_r gives me nothing. This is with PHP 5.2.n, not 5.3 btw.

Guidance, please? And thankyou!

like image 770
Dɑvïd Avatar asked May 06 '11 18:05

Dɑvïd


People also ask

How do you sort an array by key value?

To PHP sort array by key, you should use ksort() (for ascending order) or krsort() (for descending order). To PHP sort array by value, you will need functions asort() and arsort() (for ascending and descending orders).

How do you sort an array array?

To sort the array of arrays, you need to specify based on which element you want to sort them. Here we compare the arrays by their second elements. Then the sort() function loops through the array of arrays and sorts it based on the magnitude of the second elements.

How does Usort work in PHP?

The usort() function in PHP sorts a given array by using a user-defined comparison function. This function is useful in case if we want to sort the array in a new manner. This function assigns new integral keys starting from zero to the elements present in the array and the old keys are lost.

Which function is used to sort the keys of an array in ascending order?

sort() - sort arrays in ascending order. rsort() - sort arrays in descending order.


2 Answers

Your items in the array are objects, not associative arrays, so you need to refer to them like this:

function cmp($a, $b) {
  return strcmp($a->slug, $b->slug);
}
usort($allPages, 'cmp')
like image 146
Kelly Avatar answered Oct 04 '22 15:10

Kelly


Your dump of the array says that the elements are Page Objects, not arrays. By chance, do you need to say $a->created_on instead of $a['created_on']? Using object notation instead of array notation.

Just guessing...

like image 21
Tesserex Avatar answered Oct 04 '22 14:10

Tesserex