André Cardoso
André Cardoso

Reputation: 115

Help with big array

i'm new to php and i'm trying to filter some words from a string, using an array, here's the array:

$array_lugares = array
(
array("barra"=>array
(
/*SENTIDO BARRA*/
"Sao conrado"=>array("-22.999743","-43.270694"),
"Elevado do Joa"=>array("-22.999429","-43.27317")
),
"zona sul"=>array
(
/*SENTIDO ZONA SUL:*/
"passarela da barra"=>array("-23.008346","-43.303708"),
"barra grill"=>array("-23,010576", "-43,302028"),
"lagoa barra"=>array("-22,997348", "-43,263200")
),
"recreio"=>array
(
/*SENTIDO RECREIO:*/
"passarela da barra"=>array("-23.008283","-43.303634"),
"rio mar"=>array("22.999958","-43.402648"),
"ribalta"=>array("-22,999753", "-43,409211")
)));

when I do:

foreach($array_lugares[0]['zona sul'] as $lugar){
echo $lugar;
echo "</br>";
}

the output is:

Array
Array
Array

how can I make it so it shows:

barra
zona sul 
recreio

in the output, is it possible ?

Upvotes: 3

Views: 73

Answers (3)

helloandre
helloandre

Reputation: 10721

that's because $array_logares[0]['zona sul'] is giving you the object

array
(
/*SENTIDO ZONA SUL:*/
"passarela da barra"=>array("-23.008346","-43.303708"),
"barra grill"=>array("-23,010576", "-43,302028"),
"lagoa barra"=>array("-22,997348", "-43,263200")
)

and each element is an array (of points). If you want the names (instead of the array of points) you would do this:

foreach(array_keys($array_logares[0]['zona sul']) as $lugar)

if you want the name and the point, you would do this:

foreach($array_lugares[0] as $name => $lugar)

Upvotes: 0

Ibu
Ibu

Reputation: 43810

That is because you have a multidimensional array, you can loop through $lugar; also, and it will give you the correct output

Update:

foreach($array_lugares[0]['zona sul'] as $lugar){
   foreach ($lugar as $value) {
     // further inside the array

   }

 echo "</br>"; 

} 

but i think you should revisit the code you have and see if this is really the way you want to work with your data

Upvotes: 1

haknick
haknick

Reputation: 1920

 foreach($array_lugares[0] as $k => $lugar){
   echo $k;
   echo "</br>";
 }

Upvotes: 3

Related Questions