My code is :
<?php
$arr = array();
array_push($arr,"One","Two","Three");
print_r($arr);
echo '<br>';
echo json_encode($arr);
?>
As you can see, I'm passing a non-associative array to json_encode()
. The Output I get is
Array ( [0] => One [1] => Two [2] => Three )
["One","Two","Three"]
What exactly is the second line of the output? If we pass an associative array to json_encode()
, what is returned is a JSON Object, but this array that is returned is definitely does not look like a JSON object. So what is it?
Also, is there a way to convert a non-associative array to a JSON Object using json_encode()
?
If you're trying to get it in proper object notation, try this:
echo json_encode($arr,JSON_FORCE_OBJECT);
Output:
{"0":"One", "1":"Two", "2":"Three"}
Refer to json_encode() options
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