English 中文(简体)
提取下、中、上距离阵列的距离
原标题:Extracting Lower, Middle, and Higher Distancefrom Array
  • 时间:2012-05-27 13:32:39
  •  标签:
  • php
  • arrays

目标是提取最低、中、高距离。 最低距离已经成功实现。 然而,中、高距离则更为复杂。

Array content is shown below: Array name: $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){


            }
    }

}
最佳回答

Why not sort the array by value and get the min/mid/max from that? If you truly want the closest to middle element you would have to compare the 2 middle-most elements in the case of an even number of distances. Also, I didn t make use of them here, but take note of PHPs min and max functions: 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];                                                                                                                
}
问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签