<?php
// Codificar los datos.
$json = json_encode(
array(
1 => array(
'Inglés' => array(
'One',
'January'
),
'Francés' => array(
'Une',
'Janvier'
)
)
)
);
// Definir los errores.
$constantes = get_defined_constants(true);
$errores_json = array();
foreach ($constantes["json"] as $nombre => $valor) {
if (!strncmp($nombre, "JSON_ERROR_", 11)) {
$errores_json[$valor] = $nombre;
}
}
// Mostrar los errores para diferentes profundidades.
foreach (range(4, 3, -1) as $profundidad) {
var_dump(json_decode($json, true, $profundidad));
echo 'Último error: ', $errores_json[json_last_error()], PHP_EOL, PHP_EOL;
}
?>
El resultado del ejemplo sería:
array(1) {
[1]=>
array(2) {
["Inglés"]=>
array(2) {
[0]=>
string(3) "One"
[1]=>
string(7) "January"
}
["Francés"]=>
array(2) {
[0]=>
string(3) "Une"
[1]=>
string(7) "Janvier"
}
}
}
Último error: JSON_ERROR_NONE
NULL
Último error: JSON_ERROR_DEPTH