Home > Software engineering >  How to extract a key in array in php
How to extract a key in array in php

Time:06-09

I made this php code, I need to extract the value of the key "nombre_ubigeo" from the content of the array

$str = json_decode(file_get_contents("https://raw.githubusercontent.com/joseluisq/ubigeos-peru/master/json/provincias.json"));
//dd($str);
foreach ($str as $key => $value) {
    //$count = count($value);
    $ciudad[] = $value;
}

Here is a shortened JSON data sample which is representative of the overall structure:

{
  "2534": [
    {
      "id_ubigeo": "2557",
      "nombre_ubigeo": "Bagua",
      "codigo_ubigeo": "02",
      "etiqueta_ubigeo": "Bagua, Amazonas",
      "buscador_ubigeo": "bagua amazonas",
      "numero_hijos_ubigeo": "5",
      "nivel_ubigeo": "2",
      "id_padre_ubigeo": "2534"
    },
    {
      "id_ubigeo": "2563",
      "nombre_ubigeo": "Bongara",
      "codigo_ubigeo": "03",
      "etiqueta_ubigeo": "Bongara, Amazonas",
      "buscador_ubigeo": "bongara amazonas",
      "numero_hijos_ubigeo": "12",
      "nivel_ubigeo": "2",
      "id_padre_ubigeo": "2534"
    },
    {
      "id_ubigeo": "2535",
      "nombre_ubigeo": "Chachapoyas",
      "codigo_ubigeo": "01",
      "etiqueta_ubigeo": "Chachapoyas, Amazonas",
      "buscador_ubigeo": "chachapoyas amazonas",
      "numero_hijos_ubigeo": "21",
      "nivel_ubigeo": "2",
      "id_padre_ubigeo": "2534"
    },
    {
      "id_ubigeo": "2576",
      "nombre_ubigeo": "Condorcanqui",
      "codigo_ubigeo": "04",
      "etiqueta_ubigeo": "Condorcanqui, Amazonas",
      "buscador_ubigeo": "condorcanqui amazonas",
      "numero_hijos_ubigeo": "3",
      "nivel_ubigeo": "2",
      "id_padre_ubigeo": "2534"
    },
    {
      "id_ubigeo": "2580",
      "nombre_ubigeo": "Luya",
      "codigo_ubigeo": "05",
      "etiqueta_ubigeo": "Luya, Amazonas",
      "buscador_ubigeo": "luya amazonas",
      "numero_hijos_ubigeo": "23",
      "nivel_ubigeo": "2",
      "id_padre_ubigeo": "2534"
    },
    {
      "id_ubigeo": "2604",
      "nombre_ubigeo": "Rodriguez de Mendoza",
      "codigo_ubigeo": "06",
      "etiqueta_ubigeo": "Rodriguez de Mendoza, Amazonas",
      "buscador_ubigeo": "rodriguez de mendoza amazonas",
      "numero_hijos_ubigeo": "12",
      "nivel_ubigeo": "2",
      "id_padre_ubigeo": "2534"
    },
    {
      "id_ubigeo": "2617",
      "nombre_ubigeo": "Utcubamba",
      "codigo_ubigeo": "07",
      "etiqueta_ubigeo": "Utcubamba, Amazonas",
      "buscador_ubigeo": "utcubamba amazonas",
      "numero_hijos_ubigeo": "7",
      "nivel_ubigeo": "2",
      "id_padre_ubigeo": "2534"
    }
  ],
  "2625": [
    {
      "id_ubigeo": "2639",
      "nombre_ubigeo": "Aija",
      "codigo_ubigeo": "02",
      "etiqueta_ubigeo": "Aija, Ancash",
      "buscador_ubigeo": "aija ancash",
      "numero_hijos_ubigeo": "5",
      "nivel_ubigeo": "2",
      "id_padre_ubigeo": "2625"
    },
    {
      "id_ubigeo": "2645",
      "nombre_ubigeo": "Antonio Raymondi",
      "codigo_ubigeo": "03",
      "etiqueta_ubigeo": "Antonio Raymondi, Ancash",
      "buscador_ubigeo": "antonio raymondi ancash",
      "numero_hijos_ubigeo": "6",
      "nivel_ubigeo": "2",
      "id_padre_ubigeo": "2625"
    },
    {
      "id_ubigeo": "2652",
      "nombre_ubigeo": "Asuncion",
      "codigo_ubigeo": "04",
      "etiqueta_ubigeo": "Asuncion, Ancash",
      "buscador_ubigeo": "asuncion ancash",
      "numero_hijos_ubigeo": "2",
      "nivel_ubigeo": "2",
      "id_padre_ubigeo": "2625"
    },
    {
      "id_ubigeo": "2655",
      "nombre_ubigeo": "Bolognesi",
      "codigo_ubigeo": "05",
      "etiqueta_ubigeo": "Bolognesi, Ancash",
      "buscador_ubigeo": "bolognesi ancash",
      "numero_hijos_ubigeo": "15",
      "nivel_ubigeo": "2",
      "id_padre_ubigeo": "2625"
    },
    {
      "id_ubigeo": "2671",
      "nombre_ubigeo": "Carhuaz",
      "codigo_ubigeo": "06",
      "etiqueta_ubigeo": "Carhuaz, Ancash",
      "buscador_ubigeo": "carhuaz ancash",
      "numero_hijos_ubigeo": "11",
      "nivel_ubigeo": "2",
      "id_padre_ubigeo": "2625"
    },
    {
      "id_ubigeo": "2683",
      "nombre_ubigeo": "Carlos Fermin Fitzcarrald",
      "codigo_ubigeo": "07",
      "etiqueta_ubigeo": "Carlos Fermin Fitzcarrald, Ancash",
      "buscador_ubigeo": "carlos fermin fitzcarrald ancash",
      "numero_hijos_ubigeo": "3",
      "nivel_ubigeo": "2",
      "id_padre_ubigeo": "2625"
    },
    {
      "id_ubigeo": "2687",
      "nombre_ubigeo": "Casma",
      "codigo_ubigeo": "08",
      "etiqueta_ubigeo": "Casma, Ancash",
      "buscador_ubigeo": "casma ancash",
      "numero_hijos_ubigeo": "4",
      "nivel_ubigeo": "2",
      "id_padre_ubigeo": "2625"
    },
    {
      "id_ubigeo": "2692",
      "nombre_ubigeo": "Corongo",
      "codigo_ubigeo": "09",
      "etiqueta_ubigeo": "Corongo, Ancash",
      "buscador_ubigeo": "corongo ancash",
      "numero_hijos_ubigeo": "7",
      "nivel_ubigeo": "2",
      "id_padre_ubigeo": "2625"
    }
  ]
}

Is there a function to extract by name of the key in the foreach?

Resume: In this file https://raw.githubusercontent.com/joseluisq/ubigeos-peru/master/json/provincias.json there are a total of 194 values in the key "nombre_ubigeo" I need to extract them all.

CodePudding user response:

Try this

<?php
$provincias = json_decode(file_get_contents("https://raw.githubusercontent.com/joseluisq/ubigeos-peru/master/json/provincias.json"), true);

$list = [];

foreach ($provincias as $id => $values) {
    $list = array_merge($list, array_map(function ($provincia) {
        return $provincia['nombre_ubigeo'];
    }, current($provincias)));
}
print_r(count($list));
print_r($list);

CodePudding user response:

You could map array_column over the groups and merge all the results of that together like this:

array_merge(...array_map(fn($group) => array_column($group, 'nombre_ubigeo'), $data));

but I think the simplest way would be to just add an inner foreach loop.

foreach ($data as $values) {
    foreach($values as $value) {
        $ciudad[] = $value['nombre_ubigeo'];
    }
}
  • Related