Other answers work, but it's good to know that the generated JSON will have the following form (in this example, I use the hypothetical "name" field for your clients):
{ "5587d2c3cd8348455b26feab": { "_id": { "$id": "5587d2c3cd8348455b26feab" }, "name": "Robert" }, "5587d2c3cd8348455b26feac": { "_id": { "$id": "5587d2c3cd8348455b26feac" }, "name": "John" } }
Therefore, if you do not want Object _id be the key of each of your result objects, you can add the false parameter to iterator_to_array . Your code will look like this:
echo json_encode(iterator_to_array($customers, false), true);
This creates the same result as
$result = Array(); foreach ($customers as $entry) { array_push($result, $entry); } echo json_encode($result, true);
which is an array of JSON objects
[ { "_id": { "$id": "5587d2c3cd8348455b26feab" }, "name": "Robert" }, { "_id": { "$id": "5587d2c3cd8348455b26feac" }, "name": "John" } ]
Archangel
source share