使用php查找数组数组的最短距离
我想比较从 array[0] 到数组末尾的距离并显示其键的最短距离。
大批 ( [0] =>大批 ( [城市] =>大批 ( [0] =>瑞杜特 [1] =>居尔皮普 )
[distance] => 14.4
)
[1] => Array
(
[city] => Array
(
[0] => Array
(
[0] => Reduit
[1] => Ebe
)
[1] => Bees Village
[2] => Phoen Trunk Rd
[3] => Riv,Phoenix
[4] => St l Rd
[5] => Geoes Guibert St
[6] => Curepipe
)
[distance] => 1.4
)
[2] => Array
(
[city] => Array
(
[0] => Array
(
[0] => Reduit
[1] => Riv,Phoenix
)
[1] => St l Rd
[2] => Geoes Guibert St
[3] => Curepipe
)
[distance] => 3.4
)
[3] => Array
(
[city] => Array
(
[0] => Array
(
[0] => Reduit
[1] => Ebene
)
[1] => Belles Village
[2] => Phoenix Trunk Rd
[3] => Riverside,Phoenix
[4] => St Paul Rd
[5] => Georges Guibert St
[6] => Curepipe
)
[distance] => 22.4
)
)
我使用,
$total = count($array)-1; $current = $array[0]['距离'];
for($loop=1;$loop<$total;$loop++){
$next = $array[$loop]['distance'];
$current = $next;
$current = $current;
if ($next>$current){
print_r($current);
}
}
}
要获取所有键的索引,请获取第一个索引并使用交换将其与其他值进行比较。但它不起作用。请有人修复。感谢您
I want to compare the distance from array[0] to end of array and display the shortest distance with its key.
Array
(
[0] => Array
(
[city] => Array
(
[0] => Reduit
[1] => Curepipe
)
[distance] => 14.4
)
[1] => Array
(
[city] => Array
(
[0] => Array
(
[0] => Reduit
[1] => Ebe
)
[1] => Bees Village
[2] => Phoen Trunk Rd
[3] => Riv,Phoenix
[4] => St l Rd
[5] => Geoes Guibert St
[6] => Curepipe
)
[distance] => 1.4
)
[2] => Array
(
[city] => Array
(
[0] => Array
(
[0] => Reduit
[1] => Riv,Phoenix
)
[1] => St l Rd
[2] => Geoes Guibert St
[3] => Curepipe
)
[distance] => 3.4
)
[3] => Array
(
[city] => Array
(
[0] => Array
(
[0] => Reduit
[1] => Ebene
)
[1] => Belles Village
[2] => Phoenix Trunk Rd
[3] => Riverside,Phoenix
[4] => St Paul Rd
[5] => Georges Guibert St
[6] => Curepipe
)
[distance] => 22.4
)
)
i use,
$total = count($array)-1;
$current = $array[0]['distance'];
for($loop=1;$loop<$total;$loop++){
$next = $array[$loop]['distance'];
$current = $next;
$current = $current;
if ($next>$current){
print_r($current);
}
}
}
to get the index of all key, take the first index and compare it to other value using swaping.. but it doesn't work. someone has a fix please. thanks you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话,你不应该设置 $current = $next 除非它在 if 语句中,而且看起来你那里还有一个额外的 } 。我也相信你想改变你的>到一个<在你的 if 语句中。这样它就会判断 $next 是否是两者中较小的一个。
试试这个:
希望它能帮助你!
If I am understanding this correctly, you should not set $current = $next unless it is in the if statement, and it also appears you have an extra } in there. I also believe you want to change your > to a < in your if statement. This way it will tell if $next is the lesser of the two.
Try this:
Hopefully it helps you out!