How do I `json_encode()` keys from PHP array?

You can force that json_encode uses an object although you’re passing an array with numeric keys by setting the JSON_FORCE_OBJECT option:

json_encode($thearray, JSON_FORCE_OBJECT)

Then the returned value will be a JSON object with numeric keys:

{"0":1691864,"1":7944458,"2":9274078,"3":1062072,"4":8625335,"5":8255371,"6":5476104,"7":6145446,"8":7525604,"9":5947143}

But you should only do this if an object is really required.

Leave a Comment