You can use usort() .
$data = json_decode($json); usort($data, function($a, $b) { if ($a->id == $b->id) { return 0; } return $a->id < $b->id ? -1 : 1; });
Or, if you are using PHP <5.3, you need to define a comparison function, since support for anonymous functions was added in PHP 5.3.
function cmp($a, $b) { if ($a->id == $b->id) { return 0; } return $a->id < $b->id ? -1 : 1; } $data = json_decode($json); usort($data, 'cmp');
This will sort the array. After that, if you want to create an array with only the name values, you can do this with foreach .
$result = array(); foreach ($data as $entry) { $result[] = array('name' => $entry->name); }
Now the variable $result will contain:
array(array('name' => 'aaa'), array('name' => 'eee'), array('name' => 'ccc'), array('name' => 'bbb'), array('name' => 'ddd'));
Then, to encode the result again as JSON, you can call json_encode() .
echo json_encode($result);
As a result, you will get a line similar to:
[{"name": "aaa"}, {"name": "eee"}, {"name": "ccc"}, {"name": "bbb"}, {"name": "ddd"}]