Multidimensional associative array

0

I have the following code:

$lugares=array("lug"=>array("colonia"=>"sanfernanado", "direccion"=>23));

My question is how can I access the results, for example try to access the index colonia and show me the corresponding result. and I would also like to add a new index with its respective result in the second array

    
asked by Oliver Peres 17.11.2017 в 05:11
source

2 answers

1

You could access the data in the following way:

<?php

$lugares=array("lug"=>array("colonia"=>"sanfernanado", "direccion"=>23));

echo $lugares['lug']['colonia']; // Imprime: sanfernanado
echo $lugares['lug']['direccion']; // Imprime: 23
    
answered by 18.11.2017 в 05:02
0

As it is an Array 2D could have 2 foreach to get the data,

foreach ($lugares as $key => $value) { // $key tendrá el valor de "lug" y value el array
    foreach($value as $k=>$v){ // Con value hacemos referencia al array más interno
    // $k hace referencia a las claves colonia y dirección , y $v hace referencia a 
   // los valores sanfernando y 23
        echo $v. "<br>"; 
    }
}

To add a new element, you would have the following syntax, specifying the name of the array and in brackets la nueva clave , which will be equal to the new Array .

$lugares["sluggg"] = array("colonia"=>"SanFranciso", "direccion"=>554) ;
    
answered by 17.11.2017 в 05:58