I am using json_encode() to encode array into json format. but it returning object instead of array. I want to return an array not an object. any body have any idea?
Asked
Active
Viewed 1.5k times
11
-
What does the array look like? – urban_raccoons Sep 07 '13 at 06:12
-
1please check https://stackoverflow.com/questions/11195692/json-encode-sparse-php-array-as-json-array-not-json-object – Subhash Chavda Jul 09 '19 at 14:38
-
`echo json_encode(array_values($array))` - is the simplest way to force an array instead of an object. – Eugene Jul 17 '23 at 15:53
4 Answers
0
Basically json_decode() will return two types of data.
1) Object
2) Associative array
By default, json_decode() returns object type value.
But, if you want value as an array format you must use TRUE
as a second argument in json_decode().
e.g,
$decoded_value = json_decode($json_encoded_value, TRUE);

Damien
- 160
- 3
- 14
-1
actually json_encode function in php will return a json formatted string.
and if you want to parse json formatted string back in php then you should use json_decode.
json_decode function will return data two types. object & associtavie array.
json_decode(); return type object
json_decode(, TRUE); return type associtative array

developerCK
- 4,418
- 3
- 16
- 35
-1
use this code for decoding your encode json data
$encode = $your_json_encoded_data
json_decode($encode, TRUE);

sudhakar
- 582
- 1
- 3
- 13
-2
You should use json_decode
with TRUE
param like following example:
$array = array(1,2,3);
$encode = json_encode($array);
$decode = json_decode($encode, TRUE);
Now $decode
is array
, not object.

Bora
- 10,529
- 5
- 43
- 73
-
9I am asking for json_encode not for json_decode. when we use JSON data from javascript. – Deepika Patel Oct 05 '13 at 06:22