Sorting JSON array in PHP

How to sort field "name" in ASC and also sort by DESC order using only php?

That's what I have so far

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

      

What are the outputs ..

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

      

But that's what I need

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

      

How do I sort the column of names alphabetically after json_encode()

?

+2


source to share


1 answer


You are decoding the JSON string back to PHP. Sort it using normal PHP collation (looks like uksort . Then reinstall it.



Serializing data to JSON is what you do to transfer data, not perform operations on it.

+5


source







All Articles