PHP - Recorrer array

 
Vista:

Recorrer array

Publicado por Diego Forero (27 intervenciones) el 15/05/2019 18:30:23
Hola amigos..

Hice un json_decode a un JSON de respuesta de un api, guarde el resultado en un variable y le hice un var_dump.
Me da como resultado varios como esto

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
array (size=12)
  0 =>
    array (size=9)
      'id' => int 1
      'label_img' => string 'iVBORw0KGgo' (length=11)
      'action' =>
        array (size=6)
          'id' => int 11
          'data_master_id' => int 10
          'name' => string 'Entrada' (length=7)
          'description' => string 'Inicio Jornada laboral' (length=22)
          'resource' => string 'main_entry' (length=10)
          'enabled' => boolean true
      'warehouse_id' => int 153
      'created_at' => string '2019-05-06 12:13:37' (length=19)
      'updated_at' => string '2019-05-06 12:13:37' (length=19)
      'enabled' => boolean true
      'people' =>
        array (size=9)
          'id' => int 1
          'names' => string 'Andres Fernando' (length=15)
          'last_names' => string 'Serrano Pineda' (length=14)
          'address' => string 'Faca' (length=4)
          'phone' => string '3143671191' (length=10)
          'email' => null
          'document_id' => string '1070957377' (length=10)
          'created_at' => string '2019-05-06 12:00:31' (length=19)
          'updated_at' => string '2019-05-06 12:00:31' (length=19)
      'warehouse' =>
        array (size=7)
          'id' => int 153
          'warehouse' => string '530' (length=3)
          'name' => string 'Swarovski Falabella Titán' (length=26)
          'store' => boolean true
          'enabled' => boolean true
          'created_at' => string '2019-05-06 11:37:09' (length=19)
          'updated_at' => string '2019-05-06 11:37:09' (length=19)
  1 =>
    array (size=9)
      'id' => int 2
      'label_img' => string 'iVBORw0KGgoAAAANSUhEUgAAAKAAAAB4CAIAAAD6wG44AABugUlEQVR42pS9hXtbV9runb/gXOe8M6U0YCbZAovBtmRZsmRmphhiRpkZY4hjiO04cRw7zMzYQLkpTNuBtjOF6ZSnU57iTHWevZ+tpeUtp+f75rpnXUtbsud9/dP90NpS1vTXFbeUZHbWlQ+01PW31oN6Wxt6WmCt62mphZXd1IO6G2tBXbaqzobKjvqK7sbq/rbagfa6gbYG/EFUX0tdXwtcsfW1Mb+ku62uq7W2u62xq9XW0VwPKwge9rTbUL0dsG8C9XW29HY0g2AD6u9q7etmNNDbTjTU37lloGuwrwM2g4NdQ0PdgwM9Q4O9oC1DfaCR4QHU6Mggqy1jo8OoUVjHRrZuHcUVHsKKGh8fm5gYm5zcCuv4+MjExDgILhLBw8nJbfAsvmzr1uGt42OgMfhxdgOamNy2bWIcxdu7anJqAjQ1PUlWEFyH/fbtU6CpqYnp6cmV2jY1Nb59'... (length=37796)
      'action' =>
        array (size=6)
          'id' => int 14
          'data_master_id' => int 10
          'name' => string 'Salida Almuerzo' (length=15)
          'description' => string 'Hora de salida almuerzo' (length=23)
          'resource' => string 'lunch_off' (length=9)
          'enabled' => boolean true
      'warehouse_id' => int 153
      'created_at' => string '2019-05-06 13:14:11' (length=19)
      'updated_at' => string '2019-05-06 13:14:11' (length=19)
      'enabled' => boolean true
      'people' =>
        array (size=9)
          'id' => int 1
          'names' => string 'Stiven Agudelo' (length=15)
          'last_names' => string 'Serrano Pineda' (length=14)
          'address' => string 'dir xxx aca' (length=4)
          'phone' => string '31895559991' (length=10)
          'email' => null
          'document_id' => string '19559822277' (length=10)
          'created_at' => string '2019-05-06 12:00:31' (length=19)
          'updated_at' => string '2019-05-06 12:00:31' (length=19)
      'warehouse' =>
        array (size=7)
          'id' => int 153
          'warehouse' => string '530' (length=3)
          'name' => string 'la felin' (length=26)
          'store' => boolean true
          'enabled' => boolean true
          'created_at' => string '2019-05-06 11:37:09' (length=19)
          'updated_at' => string '2019-05-06 11:37:09' (length=19)

Estoy tratando de entender cómo puedo obtener cada dato y no entiendo cómo alguien me puede orientar cómo recorro este tipo de arreglos.

De antemano gracias ..
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 1.071
Bronce
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Recorrer array

Publicado por Yamil Bracho (888 intervenciones) el 15/05/2019 19:08:55
Cuando ves en la salida array (size=x), quiere decir :

- El elemento es un arreglo
- Tiene x numero de elementos

Por ejemplo
- warehouse_id del primer item
echo $data[0]['warehouse_id']

- Nombre de ese warehouse
echo $data[0]['warehouse']['name']

- Nombre y apellido de la segunda persona en el arreglo
echo $data[1]['people']['names'] . ' ' . $data[1]['people']['last_names']
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar