我是PHP新手,我正在尝试从字符串中过滤一些单词,使用数组,这里是数组:
$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")
)));
当我这样做时:
foreach($array_lugares[0]['zona sul'] as $lugar){
echo $lugar;
echo "</br>";
}
输出为:
Array
Array
Array
我怎样才能让它显示:
barra
zona sul
recreio
foreach($array_lugares[0] as $k => $lugar){
echo $k;
echo "</br>";
}
这是因为你有一个多维数组,你也可以循环$lugar;
,它会给你正确的输出
foreach($array_lugares[0]['zona sul'] as $lugar){
foreach ($lugar as $value) {
// further inside the array
}
echo "</br>";
}
但是我认为你应该重新审视你的代码,看看这是否真的是你想要的方式处理你的数据
这是因为$array_logares[0]['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")
)
和每个元素是一个数组(点)。如果你想要名字(而不是点数组),你可以这样做:
foreach(array_keys($array_logares[0]['zona sul']) as $lugar)
如果你想要名字和的点,你可以这样做:
foreach($array_lugares[0] as $name => $lugar)