doubu1950 2013-09-01 14:00
浏览 61
已采纳

递归地从嵌套数组中删除元素以查找特定值

I have the following nested array:

Array ( 
    [0] => Array ( [0] => [1] => 51.212342,6.7834665 ) 
    [1] => Array ( [0] => 28.8.2013 01:14:06 [1] => 51.2123822,6.7834572 ) 
    [2] => Array ( [0] => 28.8.2013 15:11:53 [1] => 0,0 ) 
    [3] => Array ( [0] => 28.8.2013 15:12:16 [1] => 0,0 ) 
    [4] => Array ( [0] => 28.8.2013 15:36:06 [1] => 0,0 ) 
    [5] => Array ( [0] => 28.8.2013 15:40:13 [1] => 41.117143,16.871871 ) 
    [6] => Array ( [0] => 28.8.2013 15:40:14 [1] => 0,0 ) 
    [7] => Array ( [0] => 28.8.2013 16:03:13 [1] => 0,0 ) 
    [8] => Array ( [0] => 28.8.2013 16:11:19 [1] => 40.8205315914286,16.5500314957143 ) 
    [9] => Array ( [0] => 28.8.2013 16:11:20 [1] => 0,0 ) 
    [10] => Array ( [0] => 28.8.2013 16:11:40 [1] => 40.8205315914286,16.5500314957143 ) 
    [11] => Array ( [0] => 28.8.2013 18:11:33 [1] => 45.4304359,12.3290189 ) 
    [12] => Array ( [0] => 28.8.2013 18:11:34 [1] => 0,0 ) 
    [13] => Array ( [0] => 28.8.2013 18:11:54 [1] => 45.4304456,12.3289609 ) 
    [14] => Array ( [0] => 28.8.2013 18:11:55 [1] => 0,0 ) 
    [15] => Array ( [0] => 29.8.2013 10:07:21 [1] => 51.212394,6.7834843 ) 
            ...
);

Here I need to remove all the elements that have "0,0" as their [$n][1] value. I tried this but some of the "0,0" are still there. Why?

for ($i = 0; $i < sizeof($locations); $i++) {
    $key = array_search('0,0', $locations[$i]);
    if ($key !== false) {
        unset($locations[$i]);
        $locations = array_values($locations);
    }
}

Array ( 
    [0] => Array ( [0] => [1] => 51.212342,6.7834665 ) 
    [1] => Array ( [0] => 28.8.2013 01:14:06 [1] => 51.2123822,6.7834572 ) 
    [2] => Array ( [0] => 28.8.2013 15:12:16 [1] => 0,0 ) 
    [3] => Array ( [0] => 28.8.2013 15:40:13 [1] => 41.117143,16.871871 ) 
    [4] => Array ( [0] => 28.8.2013 16:03:13 [1] => 0,0 ) 
    [5] => Array ( [0] => 28.8.2013 16:11:19 [1] => 40.8205315914286,16.5500314957143 ) 
    [6] => Array ( [0] => 28.8.2013 16:11:40 [1] => 40.8205315914286,16.5500314957143 ) 
    [7] => Array ( [0] => 28.8.2013 18:11:33 [1] => 45.4304359,12.3290189 ) 
    [8] => Array ( [0] => 28.8.2013 18:11:54 [1] => 45.4304456,12.3289609 ) 
    [9] => Array ( [0] => 29.8.2013 10:07:21 [1] => 51.212394,6.7834843 ) 
    [10] => Array ( [0] => 29.8.2013 10:07:56 [1] => 51.2123948,6.7834622 ) 
    [11] => Array ( [0] => 29.8.2013 11:57:45 [1] => 51.21244537,6.78355515 ) 
    [12] => Array ( [0] => 29.8.2013 11:58:27 [1] => 51.21238401,6.78352698 ) 
    [13] => Array ( [0] => 29.8.2013 12:01:17 [1] => 51.2124044633333,6.78353637 ) 
    [14] => Array ( [0] => 29.8.2013 12:11:18 [1] => 51.2124044633333,0.783536 ) 
    [15] => Array ( [0] => 29.8.2013 12:12:39 [1] => 51.212416045,6.783523 ) 
    ...
);
  • 写回答

2条回答 默认 最新

  • doulun1915 2013-09-01 14:44
    关注

    One problem with your code is that sizeof($locations) changes every time you do a unset. So whenever you have two consecutive [$n][1] having "0,0", you are not able to detect that. Also your code is not looking at just[$n][1], its looking at all indexes in [$n][]

    Use the below code:

    $count = count($locations);
    for ($i = 0; $i < $count; $i++) {
        if ($locations[$i][1] == "0,0") {
           unset($locations[$index]);
        }
    }
    
    $locations = array_values($locations);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?