2

How do I sort the "name" field in ASC and also sort in DESC ordering using only php?

Here is what I have thus far

$stud = json_encode($arr);
print_r($stud);

Which outputs..

[{"id":1, "name":"Some Name"},
 {"id":4, "name":"Another Name"},
 {"id":9, "name":"Third Name"}]

But this is what I need

[{"id":4, "name":"Another Name"},
 {"id":1, "name":"Some Name"},
 {"id":9, "name":"Third Name"}];

How would I sort the name column alphabetically after json_encode()?

1
  • 3
    I suggest you sort it before encoding it. Commented Sep 14, 2009 at 14:37

1 Answer 1

5

You decode the JSON string back into PHP. Sort it using the normal PHP sort routines (looks like uksort is the right one here). Then reencode it.

Serializing data to JSON is something you do in order to transport the data, not to perform operations on it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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