php - 我的数组发生了什么?项目消失

标签 php arrays sorting

我的结果数组在这里发生了什么?我希望结果数组的大小等于输入数组,但它缺少 3 个条目。它们去哪里了?他们没有去 $ret ,因为他们应该去..

代码:

<?php
init();
$list_xy = array(
    0 => array(
        'id' => '308',
        'x' => '37',
        'y' => '63'
    ),
    1 => array(
        'id' => '963',
        'x' => '38',
        'y' => '134'
    ),
    2 => array(
        'id' => '385',
        'x' => '39',
        'y' => '132'
    ),
    3 => array(
        'id' => '1231',
        'x' => '50',
        'y' => '199'
    ),
    4 => array(
        'id' => '788',
        'x' => '51',
        'y' => '59'
    ),
    5 => array(
        'id' => '1151',
        'x' => '53',
        'y' => '61'
    ),
    6 => array(
        'id' => '671',
        'x' => '55',
        'y' => '60'
    ),
    7 => array(
        'id' => '1487',
        'x' => '55',
        'y' => '55'
    )
);
$sorted_list_xy = sort_by_xy_distance($list_xy);
$sorted_list_xy_size = count($sorted_list_xy, COUNT_NORMAL);
$list_xy_size = count($list_xy, COUNT_NORMAL);
var_dump($sorted_list_xy_size == $list_xy_size ? "looks right" : "something is wrong", $sorted_list_xy_size, $list_xy_size);
die("died");


function sort_by_xy_distance($input_list)
{
    $ret = array();
    $a = $input_list[0];
    array_push($ret, $input_list[0]);
    $input_list[0] = null;
    $i = 1;
    for ($i = 1; $i < count($input_list); ++$i) {
        if ($input_list[$i] == null) {
            echo 'already added to list..';
            continue;
        }
        $ii = 1;
        $tmpdistance = 0;
        $nearest = array(
            'index' => -1,
            'distance' => PHP_INT_MAX
        );
        for ($ii = 1; $ii < count($input_list); ++$ii) {
            if ($input_list[$ii] == null || $ii == $i) {
                //echo 'already added to list..';
                continue;
            }
            $tmpdistance = abs($input_list[$ii]['x'] - $a['x']) + abs($input_list[$ii]['y'] - $a['y']);
            if ($tmpdistance < $nearest['distance']) {
                $nearest['index'] = $ii;
                $nearest['distance'] = $tmpdistance;
            }
        }
        assert($nearest['index'] != -1);
        array_push($ret, $input_list[$nearest['index']]);
        $a = $input_list[$nearest['index']];
        $input_list[$nearest['index']] = null;
    }
    return $ret;
}





function init()
{
    error_reporting(E_ALL);
    set_error_handler("exception_error_handler");
}
function exception_error_handler($errno, $errstr, $errfile, $errline)
{
    if (!(error_reporting() & $errno)) {
        // This error code is not included in error_reporting
        return;
    }
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}

输出:

already added to list..already added to list..already added to list..string(18) "something is wrong" int(5) int(8) died

预期输出:(类似于)

already added to list..already added to list..already added to list..string(11) "looks right" int(8) int(8) died

我希望列表变成: 一个列表,其中与下一个 [x][y] 的差异尽可能小,这将是:

array(8) {
  [0]=>
  array(3) {
    ["id"]=>
    string(3) "308"
    ["x"]=>
    string(2) "37"
    ["y"]=>
    string(2) "63"
  }
  [1]=>
  array(3) {
    ["id"]=>
    string(3) "788"
    ["x"]=>
    string(2) "51"
    ["y"]=>
    string(2) "59"
  }
  [2]=>
  array(3) {
    ["id"]=>
    string(4) "1151"
    ["x"]=>
    string(2) "53"
    ["y"]=>
    string(2) "61"
  }
  [3]=>
  array(3) {
    ["id"]=>
    string(3) "671"
    ["x"]=>
    string(2) "55"
    ["y"]=>
    string(2) "60"
  }
  [4]=>
  array(3) {
    ["id"]=>
    string(4) "1487"
    ["x"]=>
    string(2) "55"
    ["y"]=>
    string(2) "55"
  }
  [5]=>
  array(3) {
    ["id"]=>
    string(3) "385"
    ["x"]=>
    string(2) "39"
    ["y"]=>
    string(3) "132"
  }
  [6]=>
  array(3) {
    ["id"]=>
    string(3) "963"
    ["x"]=>
    string(2) "38"
    ["y"]=>
    string(3) "134"
  }
  [7]=>
  array(3) {
    ["id"]=>
    string(4) "1231"
    ["x"]=>
    string(2) "50"
    ["y"]=>
    string(3) "199"
  }
}

我不擅长制作图形插图,但我会试一试。 这是我的 map : map http://imagizer.imageshack.us/a/img633/3521/E0HGRe.png

我需要访问所有的黑点。 这是我目前的路径: enter image description here

这条路不是很理想.. 这是我想要的路径: enter image description here

这就是排序函数试图找到的,访问所有黑点的最短路径。

最佳答案

距离计算你应该使用毕达哥拉斯定理(或者只是hypot,就像提到的@prodigitalson)。对于地理坐标,您可以使用:http://www.movable-type.co.uk/scripts/latlong.html

让我们修复您的代码。第一个循环的每一步都应该从输入值中获取一个值并将其放入结果中。但它在内部继续,因此第一个循环的某些步骤没有完成它们的工作。尝试先删除继续。

$list_xy = array(
    0 => array(
        'id' => '308',
        'x' => '37',
        'y' => '63'
    ),
    1 => array(
        'id' => '963',
        'x' => '38',
        'y' => '134'
    ),
    2 => array(
        'id' => '385',
        'x' => '39',
        'y' => '132'
    ),
    3 => array(
        'id' => '1231',
        'x' => '50',
        'y' => '199'
    ),
    4 => array(
        'id' => '788',
        'x' => '51',
        'y' => '59'
    ),
    5 => array(
        'id' => '1151',
        'x' => '53',
        'y' => '61'
    ),
    6 => array(
        'id' => '671',
        'x' => '55',
        'y' => '60'
    ),
    7 => array(
        'id' => '1487',
        'x' => '55',
        'y' => '55'
    )
);
$sorted_list_xy = sort_by_xy_distance($list_xy);
$sorted_list_xy_size = count($sorted_list_xy, COUNT_NORMAL);
$list_xy_size = count($list_xy, COUNT_NORMAL);
var_dump($sorted_list_xy_size == $list_xy_size ? "looks right" : "something is wrong", $sorted_list_xy_size, $list_xy_size);
die("died");

function sort_by_xy_distance($input_list)
{
    $ret = array();
    $a = $input_list[0];
    array_push($ret, $input_list[0]);
    $input_list[0] = null;
    $i = 1;
    for ($i = 1; $i < count($input_list); ++$i) {
//                                                         up here
//        if ($input_list[$i] == null) {
//            echo 'already added to list..';
//            continue;
//        }
        $ii = 1;
        $tmpdistance = 0;
        $nearest = array(
            'index' => -1,
            'distance' => PHP_INT_MAX
        );
        for ($ii = 1; $ii < count($input_list); ++$ii) {
            if ($input_list[$ii] == null || $ii == $i) {
                //echo 'already added to list..';
                continue;
            }
            $tmpdistance = abs($input_list[$ii]['x'] - $a['x']) + abs($input_list[$ii]['y'] - $a['y']);
            if ($tmpdistance < $nearest['distance']) {
                $nearest['index'] = $ii;
                $nearest['distance'] = $tmpdistance;
            }
        }
        assert($nearest['index'] != -1);
        array_push($ret, $input_list[$nearest['index']]);
        $a = $input_list[$nearest['index']];
        $input_list[$nearest['index']] = null;
    }
    return $ret;
}

关于php - 我的数组发生了什么?项目消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28814048/

相关文章:

java - 数字从字符串到 char 数组再到 int 数组

php - 我无法对出生数据进行排序

php - 我无法将我的 html 表单链接到 mysql 表?

php - 在wordpress中包含父同级文件夹中的PHP文件

php - Paypal 金额篡改

javascript - 如何仅对二维数组中的单个列进行排序

java - 如何合并N排序两个文本文件

PHP:处理内存和代码低内存使用

c++ - 指针弄乱数组元素的地址

php - while 之外的数组不起作用