0

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.

Club
  • 111
  • 1
  • 8

2 Answers2

3

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);
  • I had to use `current($provincias)` since the main array contains a key called "2534" and as its value the list of items. – Fabrizio Gargiulo Jun 08 '22 at 15:25
  • Hello, It doesn't work, it only returns the values of the array with key 0. Does not return all values – Club Jun 08 '22 at 15:25
  • Only return Array ( [0] => Bagua [1] => Bongara [2] => Chachapoyas [3] => Condorcanqui [4] => Luya [5] => Rodriguez de Mendoza [6] => Utcubamba ) – Club Jun 08 '22 at 15:25
  • 2
    I thought you only wanted the names. Can you comment with an example of result you are expecting? – Fabrizio Gargiulo Jun 08 '22 at 15:27
  • Of course, 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. – Club Jun 08 '22 at 15:29
  • What I don't get is that there are 24 keys (basically numbers) and each of them has values. Do you need to extract the name for every value? Do you need to maintain a relationship between the number keys and their "nombre_ubigeo" values? – Fabrizio Gargiulo Jun 08 '22 at 15:39
  • I just need to extract the values "nombre_ubigeo" of all keys – Club Jun 08 '22 at 15:44
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/245426/discussion-between-fabrizio-gargiulo-and-club). – Fabrizio Gargiulo Jun 08 '22 at 15:49
  • thanks for all your help Fabrizio Gargiulo @Don't Panic already made the code. – Club Jun 08 '22 at 16:23
  • 1
    It is unfortunate when new users get lots of uv's for posting a code-only answer. This incentivizes them posting more low quality answers in the future. – mickmackusa Jun 08 '22 at 23:15
1

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'];
    }
}
Don't Panic
  • 41,125
  • 10
  • 61
  • 80