I am trying to echo a json-encoded array which consist of an array but i dont know it is not letting me print that thing. Here\'s my code:
Simply Use json_decode() you will get the result you need..
$array = json_decode($json, true);
echo "<pre>"; print_r($array);
Array
(
[0] => Array
(
[Status] => 1
[NewRecord] => Array
(
[fname] => xyz
[lname] => abc
[gender] => male
)
)
[1] => Array
(
[Status] => 1
[NewRecord] => Array
(
[fname] => 123
[lname] => 456
[gender] => male
)
)
[2] => Array
(
[Status] => 1
[NewRecord] => Array
(
[fname] => demo
[lname] => vvv
[gender] => female
)
)
)
Stab into the dark: some of your database rows contain non-ASCII characters (e.g. ü, é and such). Your database connection is set to latin1
, so the data is not UTF-8 encoded. json_encode
requires UTF-8 encoded data. If you fetch enough rows, there will be rows with such non-UTF-8 data in there, and json_encode
fails. With few enough rows you happen to not hit those problematic rows.
Test this by outputting echo json_last_error_msg();
after json_encode
.
Set your database connection to UTF-8. See here how to do so: UTF-8 all the way through
The reason why your browser complains about invalid JSON when you include a print_r
is simple: because then PHP outputs a lot of garbage which isn't JSON, which the browser can't decode as JSON.