1

目标是提取最小、中间和最远距离。成功达到了最低距离。然而,中间和最高距离更复杂。

数组内容如下所示: 数组名:$array

Array
(
    [0] => Array
        (
            [city] => Array
                (
                    [0] => Reduit
                    [1] => Curepipe
                )

            [distance] => 200
        )

    [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] => 151
        )

    [2] => Array
        (
            [city] => Array
                (
                    [0] => Array
                        (
                            [0] => Reduit
                            [1] => Riv,Phoenix
                        )

                    [1] => St l Rd
                    [2] => Geoes Guibert St
                    [3] => Curepipe
                )

            [distance] =>50
        )

    [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] => 101
        )

)

谁能告诉我哪里错了。我的工作是

$current = $array[0]['distance'];

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

    $next = $array[($middleDistance)]['distance'];

    if ($next<$current){

            $current = $next;

            print_r($current);

            if($current>50&&$current<100){


            }
    }

}
4

1 回答 1

0

为什么不按值对数组进行排序并从中获取最小值/中值/最大值?如果您真的想要最接近中间的元素,则必须在偶数距离的情况下比较 2 个最中间的元素。另外,我在这里没有使用它们,但请注意 PHP 的 min 和 max 函数: http : //php.net/manual/en/function.min.php http://php.net/manual/en
/function.max.php

$arr = array(200, 151, 50, 101);                                                                                                                    

sort($arr, SORT_NUMERIC);                                                                                                                           

if (count($arr)) {                                                                                                                                  
  $min = current($arr);                                                                                                                             
  $max = end($arr);                                                                                                                                 

  $mid = $arr[round(count($arr) / 2) - 1];                                                                                                                
}
于 2012-05-27T13:52:38.310 回答