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

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

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条)

相关推荐 更多相似问题