-9

Possible Duplicates:
How to sort a multi dimensional array in PHP alphabetically?
PHP : Sort array alphabetically

I am developing an API using Codeigniter and Phils RESTserver. In this API I access a database that contains users.

I would like to sort these users before outputting them. How can I do this? I tried the below code but that does not work.

function sort_by_lastname($a, $b)
{
    return strcmp($a['user']['basic']['lastname'], $a['user']['basic']['lastname']);
}

This is my data in JSON format.

http://pastie.org/2402372

How can I alter the above to sort this output (when in PHP array format, not JSON).

Thankful for all help!

Community
  • 1
  • 1
Jonathan Clark
  • 19,726
  • 29
  • 111
  • 175

3 Answers3

1
function sort_by_lastname($a, $b) {
    $a = trim($a['user']['basic'][0]['lastname']);
    $b = trim($b['user']['basic'][0]['lastname']);

    if ($a == $b) {
        return 0;
    }

    return ($a < $b) ? -1 : 1;
}

uasort($array['contacts'],'sort_by_lastname');
AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
-1

Check out php array_multisort

http://php.net/manual/en/function.array-multisort.php

Adam Magaluk
  • 1,716
  • 20
  • 29
-2

Check PHP.net before asking here. A quick search would have turned up PHP's asort() function: http://php.net/asort

ams
  • 796
  • 1
  • 12
  • 22
  • 1
    really? Can you show us some example code? – PeeHaa Aug 26 '11 at 16:39
  • I'm not doing your work for you – ams Aug 26 '11 at 16:43
  • 1
    @andy if you keep up that attitude you'll be haunted with a 3 digit rep score. We are an example-based community and for all you know the OP tried every sort function he could find on php.net and just isn't advanced enough to sort a parent array by a child array value. – AlienWebguy Aug 26 '11 at 16:50
  • 1
    "Haunted by a 3 digit rep score?" I think I'll live. And I stand by my answer pointing the OP to PHP.net, especially given the question's current -6 score. Perhaps my response to a request for specific code was a bit harsh, but my point stands that a site like StackOverflow shouldn't be a place where bad programmers come to get their work done for them, it should be a place where all programmers come to learn how to get better. Teach a man to fish, and all that. – ams Aug 26 '11 at 16:54
  • Fair enough. Just remember, rep = privileges. If you really want to help make SO a better place, it will be much easier for you if you have a higher rep. – AlienWebguy Aug 26 '11 at 16:57
  • The other problem is that a "quick search" is very likely to turn up this very post, in fact my fourth result in Google for "php sort array aplhabetically" is an SO post. If you're going to answer the question, might as well answer the question, provide an example, and site the manual as reference. – Wesley Murch Aug 26 '11 at 17:34
  • But do you know what results 1-3 were? PHP manual documentation for sorting, array sorting, and the ``asort()`` function. If a question is so obvious that it turns up search results like that before a SO post, I think it's actually doing the poster a disservice to do their work for them. Use the first 3 search results to figure out how it works. – ams Aug 26 '11 at 17:43
  • andy, I agree with that - but if you decide to post an answer, do it in good faith with the goal of helping the OP. It's really your attitude and not so much your RTFM answer that attracted the downvotes. PeeHaa's comment implied that it is not so simple, and he is right, and you declined to show any understanding of what to actually do with this function. We're still waiting for the [general reference close reason](http://meta.stackexchange.com/questions/86043/introduce-a-general-reference-close-reason) to make it to SO (it seems to be working well on other SE sites) – Wesley Murch Aug 26 '11 at 18:13
  • Look, I appreciate all the concern, but I'm kind of at a loss about why my modest answer is receiving so many lectures. The OP asked a painfully obvious question that -- and I don't think anyone is disputing this -- could have been solved on their own with a little research. I pointed them in the right direction, included a link to the appropriate PHP document, and that was that. I'm not upset about getting downvotes -- I do understand the politics of SO, so downvotes on answers like mine are to be expected. I get that. – ams Aug 26 '11 at 19:43