Nemuri Nemuri - 1 year ago 139
JSON Question

json_encode not working as expected

I have this piece of code


$database = new Database();

$conexion = $database->getConnection();

$statement = $conexion->prepare("SELECT nombre_asignatura FROM asignaturas");


echo "<br><br><br>";

foreach ($asignaturas as $key => $value) {
echo json_encode($value);
echo "<br><br><br>";
foreach ($asignaturas as $key => $value) {
echo "<br>";

Which gives the following results


enter image description here

I cannot understand why does it work only once. Also, is there a way to use ir to encode the whole array at once? I tried to use the "depth" variable, but I don't quite get it

Answer Source

json_last_error() will help figuring out what is wrong:

As @user2864... pointed out I also guess it is the special characters that fail. Also as @Ghost pointed out, don't foreach encode.


In case this will fail debug with the json_last_error().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download