使用php查找数组数组的最短距离

发布于 2025-01-02 04:10:28 字数 2057 浏览 1 评论 0原文

我想比较从 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 技术交流群。

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

发布评论

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

评论(1

凉月流沐 2025-01-09 04:10:28

如果我理解正确的话,你不应该设置 $current = $next 除非它在 ​​if 语句中,而且看起来你那里还有一个额外的 } 。我也相信你想改变你的>到一个<在你的 if 语句中。这样它就会判断 $next 是否是两者中较小的一个。

试试这个:

for($loop=1;$loop<$total;$loop++){

    $next = $array[$loop]['distance'];

    if ($next<$current){
        $current = $next;
        print_r($current);

    }
}

希望它能帮助你!

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:

for($loop=1;$loop<$total;$loop++){

    $next = $array[$loop]['distance'];

    if ($next<$current){
        $current = $next;
        print_r($current);

    }
}

Hopefully it helps you out!

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