Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

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?

share|improve this question
 
What does the array look like? –  urban_racoons Sep 7 at 6:12
add comment

marked as duplicate by Orangepill, Ozair Kafray, Glavić, Tgr, Madara Uchiha Sep 7 at 9:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4 Answers

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.

share|improve this answer
 
I am asking for json_encode not for json_decode. when we use JSON data from javascript. –  Deepika Patel Oct 5 at 6:22
add comment

Basically json_decode() will return data two types.

1) object 
2) associtavie array

By default json_decode() return object type value.

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

e.g,

$decoded_value = json_decode($json_encoded_value, TRUE);
share|improve this answer
add comment

use this code for decoding your encode json data

$encode = $your_json_encoded_data

json_decode($encode, TRUE);
share|improve this answer
add comment

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

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.