使用array_splice导致" Undefined Offset" -error

时间:2017-02-10 20:41:19

标签: php arrays

晚上好人,

目前处理一段带有数组的代码,如果满足条件,这些数组将被删除。在使用array_unset尝试之后,我决定使用array_splice来破坏id结构。不幸的是我有同样的问题:

E_NOTICE : type 8 -- Undefined offset: 5 -- at line 59
E_NOTICE : type 8 -- Undefined offset: 5 -- at line 60

偏移量从5到2排名。

说明:为了理解我在这里所做的事情,我会做一个简短的解释。使用给定的x和y坐标,我的小程序应该遍历数组元素,以便从起始城镇找到最近的城市。最近的点将是新的起点并从cities-array中删除,因为它已被访问过。并且整个过程再次开始,直到cities-array中没有其他元素。这就像旅行商问题一样。

现在我想解决它,但我不知道我是怎么理解这个问题的。我读过有关使用array_values的内容,但它没有用。

$cities = array
  (
    (0) => Array(
      ('city') => 'San Francisco',
      ('x_cord') => '22',
      ('y_cord') => '28',
      ),
    (1) => Array(
      ('city') => 'Oakland',
      ('x_cord') => '15',
      ('y_cord') => '13',
      ),
    (2) => Array(
      ('city') => 'Stanford',
      ('x_cord') => '5',
      ('y_cord') => '2',
      ),
    (3) => Array(
      ('city') => 'Palo Alto',
      ('x_cord') => '17',
      ('y_cord') => '15',
      ),
    (4) => Array(
      ('city') => 'San Jose',
      ('x_cord') => '5',
      ('y_cord') => '2',
      ),
    (5) => Array(
      ('city') => 'Marin',
      ('x_cord') => '22',
      ('y_cord') => '28',
      ),
    (6) => Array(
      ('city') => 'Sacramento',
      ('x_cord') => '30',
      ('y_cord') => '40',
      )
);       




$curCit = "San Francisco";
$key = array_search('Oakland', array_column($cities, 'city'));
$curX = $cities[$key]['x_cord']; //3; 
$curY = $cities[$key]['y_cord']; //4;
$hv = 0;
$distance = 0;
$row_city = 0;

$way = array();
$counter = count($cities);
while ($counter>0) {

        for ($i = 5; $i >= 0; $i--) {
            global ${"value_" . $i};
            //${"value_" . $i} = $cities[$i][1]+$cities[$i][2];
            ${"value_" . $i . "x"} = $curX - $cities[$i]['x_cord'];
            ${"value_" . $i . "y"} = $curY - $cities[$i]['y_cord'];
            ${"value_" . $i} = sqrt(${"value_" . $i . "x"}*${"value_" . $i . "x"} + ${"value_" . $i . "y"}*${"value_" . $i . "y"});

            if ( $hv == 0 ) {
                //global $hv;
                $hv = ${"value_" . $i};

            }
            elseif ($hv > 0 ) {



                      if (${"value_" . $i}<$hv) {
                      //global $hv;
                      $hv = ${"value_" . $i};
                      $distance = $distance + $hv;
                      $row_city = $i;
                      $curX = $cities[$i]['x_cord']; //3; 
                      $curY = $cities[$i]['y_cord']; //4;


                               $newdata =  array (
                                      'next_city' => $cities[$i]['city'],
                                      'nxt_cty_xcord' => $cities[$i]['x_cord'],
                                      'nxt_cty_ycord' => $cities[$i]['y_cord'],
                                      'distance' => ${"value_" . $i}
                                    );
                          //array_push($way,$newdata);
      array_push($way,$newdata);
      array_splice($cities, $i);
      $cities = array_values($cities);

                      } //end 2nd if-clause

            echo number_format(${"value_" . $i},2);
            echo " ";
            echo "HV ";
            echo number_format($hv,2);
            echo "<br>";
             $hv = 0;

        } //end 1st if-clause
      } //end for-clause



      $counter--;
} //end while-loop
print_r($cities);
echo "<br>";
print_r($way);

代码还不完美,因为我还在工作。但是从cities-multidimensional数组中删除数组元素的问题阻止了我。

有人可以提供帮助并提供建议吗? 如果你想推荐array_values,请在我尝试之后做个儿子并给出一个例子,并且可能犯了错误。

提前致谢。

编辑:虽然如果我将array_splice定位在我赋予值的变量后面,那么这将是一个解决方案。

1 个答案:

答案 0 :(得分:0)

我尝试根据您的描述尽可能地简化您的代码。如果事情不清楚,请不要犹豫。我省略了&#39; next_city&#39;结果中的项目,因为它似乎没必要(由于下一项中的相同数据),但代码可以很容易地改变。

值得在审核您的代码后提及这些内容:

  • 尝试使用变量$ {xy}只在必要时使用它,它的可读性会降低,即使是在一两年后也是如此: - )
  • 当您处理索引不清晰的迭代(例如数组)时,请使用foreach循环而不是for。从阵列中删除项目时,您不会遇到未定义索引的问题。

最终代码(没有数组定义):

//for debugging
//var_dump($cities);

$startCity = "San Francisco";

$nearestCityIndex = array_search($startCity, array_column($cities, 'city'));
$hv = null;
$counter = count($cities);
for($c=0; $c < $counter; $c++) {
    //add new item to the route plan array
    $route[] = array_merge(
        $cities[$nearestCityIndex],
        array('distanceFromPrevious' => $hv)
    );

    //remove already planned city from source array
    unset($cities[$nearestCityIndex]);

    //get last one from already planned cities
    $lastItem = $route[count($route)-1];

    $hv = 99999;
    //loop through remaining cities to get shortest possible distance last planned city
    foreach($cities as $key => $item) {
        $tmpDist = sqrt(pow($lastItem['x_cord'] - $item['x_cord'],2) + pow($lastItem['y_cord'] - $item['y_cord'], 2));
        if ($hv >= $tmpDist) {
            $hv = $tmpDist;
            $nearestCityIndex = $key;
        }
    }
}

//for debugging
//echo "<br><br>";
//var_dump($route);