从阵列中提取下,中,高距离

时间:2012-05-27 13:32:40

标签: php arrays

目标是提取最小,中等和最高距离。成功实现了最低距离。然而,中间和最高距离更复杂。

数组内容如下所示: 数组名称:$ 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){


            }
    }

}

1 个答案:

答案 0 :(得分: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];                                                                                                                
}