What is different between { and [ in json
model 1:
"cell": { "id": "3961" }
model 2:
"cell": [ "id": "3961" ]
how can i transfer model 1 to 2 via json_encode ?
What is different between { and [ in json
model 1:
"cell": { "id": "3961" }
model 2:
"cell": [ "id": "3961" ]
how can i transfer model 1 to 2 via json_encode ?
{
is an Object,
[
is an Array.
See the official Docs.
In your case, the first example is a normal Object with a property called id
. The second example is an associative array with an index called id
.
JSON does not have associative arrays. The second example is invalid JSON.
This is, due to the fact that JSON is JavaScript Object Notification and JavaScript does not know associative arrays. Instead, JavaScript allows you to dynamically add new property to an Object and enables you to access any Object-property using the brackets: Object['property']
.
So, Objects are (kind of) associative arrays in JavaScript and therefor in JSON.
Since the json_encode()
-function encodes creates the JSON-String from the supplied object, you'll need to pass an object instead of an array.