帮助处理大数组

发布于 2024-11-04 14:11:01 字数 934 浏览 1 评论 0原文

我是 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

在输出中,是吗可能的 ?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏天碎花小短裙 2024-11-11 14:11:01
 foreach($array_lugares[0] as $k => $lugar){
   echo $k;
   echo "</br>";
 }
 foreach($array_lugares[0] as $k => $lugar){
   echo $k;
   echo "</br>";
 }
没︽人懂的悲伤 2024-11-11 14:11:01

那是因为你有一个多维数组,你也可以循环 $lugar; ,它会给你正确的输出

更新:

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

   }

 echo "</br>"; 

} 

但我认为你应该重新访问你拥有的代码,看看这是否是确实是您想要的数据处理方式

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

桜花祭 2024-11-11 14:11:01

这是因为 $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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文