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

7
  • 5
    -1 for research. array_sort() Commented Aug 26, 2011 at 16:32
  • 1
    for starters, you should compare $a[...] and $b[...]. Commented Aug 26, 2011 at 16:33
  • 2
    Why don't you use a database to sort them? Commented Aug 26, 2011 at 16:34
  • @Rikudo - interesting I don't know of a function called array_sort(). Even still, he wants to sort the parent array based on criteria of a nested array's value. Commented Aug 26, 2011 at 16:41
  • 1
    Well I downvoted because I tried to answer his question in his other question but instead he started a new question. Commented Aug 26, 2011 at 16:45

3 Answers 3

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');
Sign up to request clarification or add additional context in comments.

4 Comments

+1 this actually works: pastie.org/2434498
Getting really close now. This is how the array looks before it is converted to a JSON-object. pastie.org/2435409. I get this error: uasort() expects parameter 1 to be array, null given.
I also get this error: Fatal error: Cannot use object of type stdClass as array.
You're missing the assoc flag in your json_decode: json_decode($array,TRUE) where TRUE means create an associative array.
-1

Check out php array_multisort

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

Comments

-2

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

9 Comments

really? Can you show us some example code?
I'm not doing your work for you
@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.
"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.
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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.