Browse associative array with foreach in PHP

1

I want to print the colonias array in a select by means of a foreach but it does not fit.

{
  "delegacion":"\u00c1lvaro Obreg\u00f3n",
  "estado":"Ciudad de M\u00e9xico",
  "region":"Centro",
  "colonias":[
    {"idcp":"27886","asentamiento":"Ampliaci\u00f3n El Capul\u00edn"},
    {"idcp":"27887","asentamiento":"Liberales de 1857"},
    {"idcp":"27888","asentamiento":"Bel\u00e9m de las Flores"},
    {"idcp":"27889","asentamiento":"El Capul\u00edn"}
  ]
}

I managed to do it with jQuery in the following way but they ask me in PHP.

for (var i = 0; i < data.colonias.length; i++) {
  $('#colonias').append('<option value="' + data.colonias[i].idcp + '">' + data.colonias[i].asentamiento + '</option>');
}
    
asked by Carlos Enrique Gil Gil 23.10.2017 в 17:25
source

4 answers

2

You can use json_decode to create an array or a stdClass object from your json.

Reading, if using json_decode creating a stdClass would be very simple.

Seeing your code, apparently you would want options with the colonies that are in the JSON.

To obtain it you can do:

   $json='{
      "delegacion":"\u00c1lvaro Obreg\u00f3n",
      "estado":"Ciudad de M\u00e9xico",
      "region":"Centro",
      "colonias":[
        {"idcp":"27886","asentamiento":"Ampliaci\u00f3n El Capul\u00edn"},
        {"idcp":"27887","asentamiento":"Liberales de 1857"},
        {"idcp":"27888","asentamiento":"Bel\u00e9m de las Flores"},
        {"idcp":"27889","asentamiento":"El Capul\u00edn"}
      ]
    }';

  $myJson=json_decode($json);

$strHTML='<select id="colonias">'; //Si sólo quieres los option quita esto

foreach ($myJson->colonias as $k){
    $strHTML.='<option value="'.$k->idcp.'">'.$k->asentamiento.'</option>';
}

$strHTML.='</select>';            //Si sólo quieres los option quita esto

echo $strHTML;

Result:

    <select id="colonias">
    <option value="27886">
        Ampliación El Capulín
    </option>

    <option value="27887">
        Liberales de 1857
    </option>

    <option value="27888">
        Belém de las Flores
    </option>

    <option value="27889">
        El Capulín
    </option>
</select>

Previous answer with more explanations ...

<?php

$json='{
  "delegacion":"\u00c1lvaro Obreg\u00f3n",
  "estado":"Ciudad de M\u00e9xico",
  "region":"Centro",
  "colonias":[
    {"idcp":"27886","asentamiento":"Ampliaci\u00f3n El Capul\u00edn"},
    {"idcp":"27887","asentamiento":"Liberales de 1857"},
    {"idcp":"27888","asentamiento":"Bel\u00e9m de las Flores"},
    {"idcp":"27889","asentamiento":"El Capul\u00edn"}
  ]
}';

$myJson=json_decode($json);
print_r($myJson);

/*Leyendo datos*/
echo "DELEGACIÓN: ".$myJson->delegacion.PHP_EOL;
echo "ESTADO: ".$myJson->estado.PHP_EOL;
echo "REGIÓN: ".$myJson->region.PHP_EOL;

echo "COLONIAS: ".PHP_EOL;

foreach ($myJson->colonias as $k){
    echo "\t".$k->idcp." - ".$k->asentamiento.PHP_EOL;
}

?>

Here the print_r($myJson); is only informative, to see what is created.

You will see this on screen:

    stdClass Object
(
    [delegacion] => Álvaro Obregón
    [estado] => Ciudad de México
    [region] => Centro
    [colonias] => Array
        (
            [0] => stdClass Object
                (
                    [idcp] => 27886
                    [asentamiento] => Ampliación El Capulín
                )

            [1] => stdClass Object
                (
                    [idcp] => 27887
                    [asentamiento] => Liberales de 1857
                )

            [2] => stdClass Object
                (
                    [idcp] => 27888
                    [asentamiento] => Belém de las Flores
                )

            [3] => stdClass Object
                (
                    [idcp] => 27889
                    [asentamiento] => El Capulín
                )

        )

)

Result:

What is interesting is this. The final result would be something like this:

DELEGACIÓN: Álvaro Obregón
ESTADO: Ciudad de México
REGIÓN: Centro
COLONIAS: 
    27886 - Ampliación El Capulín
    27887 - Liberales de 1857
    27888 - Belém de las Flores
    27889 - El Capulín
    
answered by 23.10.2017 / 20:31
source
1

If you want you can put more information to give you a more accurate answer, but broadly speaking it would be something like this:

<select>
<?php foreach($datos as $dato){ ?>
  <option><?php echo $dato->delegacion; ?></option>
  <option><?php echo $dato->estado; ?></option>
  <option><?php echo $dato->region; ?></option>
  <option><?php echo $dato->colonia; ?></option> 
<?php  }?>
</select>

Where $ data is the variable that contains the information of your query to the DB.

    
answered by 23.10.2017 в 17:34
0

It is not very clear to me, but if it is part of a json (it is what you have there) the ideal is with the each of jquery but if they ask you with foreach it would be

data.colonias.forEach(function(element) {
//inertas los options en el select
//puedes crearlos con jquery
// o js con createElement
});

If it's direct from the php, Alvaro's answer will be fine

    
answered by 23.10.2017 в 18:57
0

You can use json_decode and go through the arrangement:

<?php
$json =
'{
  "delegacion":"\u00c1lvaro Obreg\u00f3n",
  "estado":"Ciudad de M\u00e9xico",
  "region":"Centro",
  "colonias":[
    {"idcp":"27886","asentamiento":"Ampliaci\u00f3n El Capul\u00edn"},
    {"idcp":"27887","asentamiento":"Liberales de 1857"},
    {"idcp":"27888","asentamiento":"Bel\u00e9m de las Flores"},
    {"idcp":"27889","asentamiento":"El Capul\u00edn"}
  ]
}';

$jArray = json_decode($json);

$colonias = $jArray->colonias;

?>
<select>
<?php foreach ($colonias as  $key => $value) : ?>

  <option value="<?php echo $value->idcp; ?>"><?php echo $value->asentamiento; ?></option>

<?php endforeach; ?>

</select>
    
answered by 23.10.2017 в 21:30