user2964231
user2964231

Reputation: 39

Get specific value from multi array JSON with PHP

I run into a problem, since I'm new to JSON parse in PHP...

I get the following response when calling to an api, after successful login:

{
  "result": true,
  "errors": [],
  "response": {
    "result": true,
    "count": 1,
    "more": false,
    "azafatas": [
      {
        "serverDeleted": false,
        "tstamp": "000000ff56937c7",
        "idAzafata": 3f332,
        "nombre": "Teresa",
        "apellidos": "L",
        "agencia": {
          "serverDeleted": false,
          "tstamp": "000ff000000071949d",
          "idAgencia": f1076,
          "nombre": "Agency",
          "codigo": "",
          "descripcion": "",
          "email": "[email protected]",
          "telefono": "+351 912145003"
        },
        "idAgencia": 1076,
        "alias": "Teresa L",
        "codigo": "084",
        "email": "[email protected]",
        "telefono": "9163449236",
        "direccion": {
          "serverDeleted": false,
          "tstamp": "000000d540000718157",
          "idDireccion": 66573,
          "idProvincia": 1160,
          "direccionCompleta": "Mimosa n 20",
          "posicion": {
            "longitude": -8.057900000008,
            "latitude": 42.2104936
          },
          "provincia": {
            "serverDeleted": false,
            "tstamp": "00009809000520a4f9",
            "idProvincia": 1160,
            "nombre": "PORTO",
            "codigo": "PORTO"
          },
          "codigoPostal": "4585-359",
          "descripcion": "",
          "localidad": "Paredes",
          "posicionVerificada": true
        }
      }
    ]
  }
}

I need to get the value of "nombre": "Teresa" inside a $variable.

I tryed echo $json['response']['azafatas']['nombre']; but I get "Notice: Undefined index: nombre in..."

But if I try echo $json['response']['result']; I get correct echo "1"

What I'm missing to get the value of nombre...

Best regards!

Upvotes: 0

Views: 43

Answers (1)

Ivan
Ivan

Reputation: 96

try :

$json['response']['azafatas'][0]['nombre'];

on azafatas you will see mark '[' its means the json using index like usual array...

Upvotes: 1

Related Questions