[
{
"id": "123",
"name": "aaa"
},
{
"id": "567",
"name": "bbb"
},
{
"id": "469",
"name": "ccc"
},
{
"id": "577",
"name": "ddd"
},
{
"id": "388",
"name": "eee"
}
]
How to make a order when json decode?
$data = json_decode($json);
foreach ($data as $row) {
sort($row)?
}
I need all the decode data make an order by id.
Final data output: "name": "aaa", "name": "eee", "name": "ccc", "name": "bbb", "name": "ddd"
You could use
usort().Or, if you’re using PHP < 5.3, you need to define the comparison function, since the support for anonymous functions was added in PHP 5.3.
This would sort the array. After that, if you want to create an array with only the
namevalues, you could do that with aforeach.The
$resultvariable will now contain:Then, to encode the result as JSON again, you can call
json_encode().This will output a string similar to: