It is not so much as a problem as I have fixed the issue but I am stuck on the fix, I would like to know why you need to double json_encode a multidimenail array in php when you are returning it to javascript in an ajax call for example.
My Array
$jsonData = array('foo', 'bar');
$jsonData['catOne'][] = array('foo two', 'bar two');
PHP
return json_encode(json_encode($jsonData));
asked Apr 4, 2012 at 17:42
Oliver Bayes-Shelton
6,28313 gold badges56 silver badges90 bronze badges
-
Erm... it is not needed.kirilloid– kirilloid2012年04月04日 17:44:36 +00:00Commented Apr 4, 2012 at 17:44
-
Would you be able to post the output of a single- and double-encoded result?jprofitt– jprofitt2012年04月04日 17:44:58 +00:00Commented Apr 4, 2012 at 17:44
-
well when I do a http request from titanium mobile and use single json_encode it won't load but if I use double it willOliver Bayes-Shelton– Oliver Bayes-Shelton2012年04月04日 17:51:20 +00:00Commented Apr 4, 2012 at 17:51
1 Answer 1
You don't need to. The json_encode goes through the deepest elements and the json will be perfectly encoded:
$arr = array(
1 => array(5,6,7),
2 => array(4,3,2),
);
var_dump(json_encode($arr));
The output for that is:
string(25) "{"1":[5,6,7],"2":[4,3,2]}"
If you try:
var_dump(json_encode(json_encode($arr)));
The output will be:
string(31) ""{\"1\":[5,6,7],\"2\":[4,3,2]}""
See the difference?
answered Apr 4, 2012 at 17:45
Daniel Ribeiro
10.3k12 gold badges50 silver badges80 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Oliver Bayes-Shelton
well when I do a http request from titanium mobile and use single json_encode it won't load but if I use double it will
Explore related questions
See similar questions with these tags.
default