我有以下嵌套数组:
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 )
...
);
在这里,我需要删除所有具有“0,0”作为[$n][1]
值的元素。我试过这个,但是一些“0,0”仍在那里。为什么呢?
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 )
...
);
答案 0 :(得分:1)
foreach ($locations as $index => $row) {
if ($locations[$index][1] == "0,0") unset($locations[$index]);
}
$locations = array_values($locations);
答案 1 :(得分:1)
代码的一个问题是每次执行取消设置时sizeof($ locations)都会更改。所以每当你有两个连续的[$ n] [1]有“0,0”时,你就无法检测到它。你的代码也不只是[$ n] [1],它查看[$ n] []
中的所有索引使用以下代码:
$count = count($locations);
for ($i = 0; $i < $count; $i++) {
if ($locations[$i][1] == "0,0") {
unset($locations[$index]);
}
}
$locations = array_values($locations);