Access to json in php problem

Hi, i have a url with this response:

{"status":"success","message":"","data":[{"Nit":1037613756.0,"Placa":"JBL482","Numero_OT":661134,"Ingreso":"2021-02-23T00:00:00","EnProceso":"Si","Facturado":"Si","Pagado":"No","Entregado":"No","FechaEntrega":null,"Detalle":[{"Numero_OT":661134,"Operación":"INSUMOS REV. KM.","Valor_Total":13189.9609},{"Numero_OT":661134,"Operación":"REVISION DE 35000","Valor_Total":138136.172},{"Numero_OT":661134,"Operación":"DESINFECCION","Valor_Total":1.19},{"Numero_OT":661134,"Operación":"SERVICIO CUORIER","Valor_Total":0.0},{"Numero_OT":661134,"Operación":"ACD ACEITE SUPREME 5W30 DEXOS1 G CANECA","Valor_Total":102233.75},{"Numero_OT":661134,"Operación":"FILTRO ACEITE TRACKER","Valor_Total":33765.8945},{"Numero_OT":661134,"Operación":"LIMPIADOR DE PARTES DE FRENOS","Valor_Total":23500.1816},{"Numero_OT":661134,"Operación":"BOMBILLO 2V2/ 5W","Valor_Total":18005.89},{"Numero_OT":661134,"Operación":"BOMBILLO 2V2/ 5W","Valor_Total":18005.89}],"Solicitud":[{"Numero_OT":661134,"Solicitud_Cliente":"REVISION DE 35.000 KM","Respuesta_Tecnico":"SE REALIZA CON REVISION DE FRENOS SUSPENSION LUCES Y NIVELES"},{"Numero_OT":661134,"Solicitud_Cliente":"ACEITE SINTéTICO","Respuesta_Tecnico":"SE REALIZA CAMBIO"}]}]}

I tried this code to access the items, but no luck:

$url = "";

$json = file_get_contents($url);

$obj = json_decode($json, true);

foreach ($obj as $key) {

echo "Status: " . $key["status"];


Any good advice?

foreach iterates over the values of your decoded array; in this case the first two values are "success" and "" - an empty string for the message property. So for your first loop, the value of $key will be "success". Trying to access the key "status" on a string makes no sense.

My first piece of advice would be to use $obj = json_decode($json); without the true parameter. This will give you a truer representation of the json, as an object rather than an array.

If you just want a single property from the object there’s no need to loop, you can print them out directly:

$obj = json_decode($json);
echo "Status: " . $obj->status;

You can print out the entire object to see how to access different parts. If you’re working in a browser, the <pre> tag ensures that the output is formatted nicely.

echo '</pre>';
echo '</pre>';

Thanks, but the thing is i need to get the items in “data”, any idea?

You’ve presumably seen that, if you use the method I showed above, your JSON looks something like:

    [status] => success
    [message] => 
    [data] => Array(...)

You can access data with $obj->data. You can loop over it with:

foreach ($obj->data as $item) {
Sponsor our Newsletter | Privacy Policy | Terms of Service