PHP json_encode returns strings as objects instead of arrays

I am using json_encode () to encode an array in json format. but it returns an object instead of an array. I want to return an array, not an object. does any body have any idea?

+7
json object arrays php
source share
4 answers

Basically json_decode () returns two data types.

1) Object 2) Associative array 

By default, json_decode () returns an object type value.

But, if you want the value as an array format, you must use TRUE as the second argument in json_decode ().

eg,

 $decoded_value = json_decode($json_encoded_value, TRUE); 
+3
source share

You should use json_decode with the TRUE parameter as the following example:

 $array = array(1,2,3); $encode = json_encode($array); $decode = json_decode($encode, TRUE); 

Now $decode is an array , not an object.

+1
source share

Actually the json_encode function in php will return the json format string.

and if you want to parse json formatted string in php then you should use json_decode .

Function

json_decode returns two types of data. object and associative array.

json_decode (); return type object

json_decode (, TRUE); return type:

+1
source share

use this code to decode json encoding data

 $encode = $your_json_encoded_data json_decode($encode, TRUE); 
+1
source share

All Articles