5

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!

1
  • What do you mean by created_on value.
    – SIFE
    May 6, 2011 at 18:46

3 Answers 3

4

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')
3
  • PHP uses arrows, not periods. But the idea is the same.
    – Tesserex
    May 6, 2011 at 18:48
  • Haha, sorry, I work with PHP and Ruby daily and sometimes get them mixed up when I switch so quickly. Fixed.
    – Kelly
    May 6, 2011 at 18:49
  • that cracked it! Sort of a "doh!" moment for me. Many thanks!
    – Dɑvïd
    May 6, 2011 at 20:27
3

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...

1
  • Yes, and thanks for nudging Kelly's code. It was fixed by the time I was able to get back to this. :)
    – Dɑvïd
    May 6, 2011 at 20:28
1

As @Tesserex suggests, you need to use object notation instead of array notation.

If you had notices turned on, you'd get error messages about accessing an object as an array.

Another thing to consider, is, your Pages don't all have a 'created_on' attribute, some have a 'published_on' attribute. You'll have to do some error checking / logic inside your usort method to make sure that the key you want to sort by is available, and do something when it's not.

2
  • Thanks for the heads-up on the error messages. In the environment that this code is for, it is a pain to turn on error reporting, but I ought to have taken the trouble. And well caught on the different date attributes: in fact, they all have both. That was just my mistake in trimming down the objects for presenting here.
    – Dɑvïd
    May 6, 2011 at 20:29
  • error_reporting(E_ALL); and ini_set('display_errors', 'on'); should work in most situations. not really well known methods \: May 6, 2011 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.