I have a an array setup as follows:
$myArray = array();
$myArray[] = "New array item 1";
$myArray[] = "New array item 2";
$myArray[] = "New array item 3";
When I run json_encode() on it it outputs the following:
["New array item 1","New array item 2","New array item 3"]
What I want is for the function to encode the indexes as strings:
{"0":"New array item 1","1":"New array item 2","2":"New array item 3"}
So that later I can remove say the first item without affecting the index of the second.
Is there an easy way to do this?
Use JSON_FORCE_OBJECT
:
json_encode( $data, JSON_FORCE_OBJECT );
Requires PHP 5.3+
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With