【发布时间】:2014-08-19 20:02:12
【问题描述】:
大家好,我有两个如下所示的数组:
array(3) {
["15PCW"]=>
array(4) {
["longitude"]=>
string(17) "-88.3511517556820"
["latitude"]=>
string(16) "35.9298720640290"
["knownLocationTag"]=>
string(7) "15PCW"
["count"]=>
string(1) "0"
}
["12RCD"]=>
array(4) {
["longitude"]=>
string(17) "-88.4423244521810"
["latitude"]=>
string(16) "34.0964334531290"
["knownLocationTag"]=>
string(6) "12RCD"
["count"]=>
string(1) "0"
}
["43RICK"]=>
array(4) {
["longitude"]=>
string(17) "-88.5154700586220"
["latitude"]=>
string(16) "34.0726750453080"
["knownLocationTag"]=>
string(11) "43RICK"
["count"]=>
string(1) "0"
}
}
array(3) {
[12]=>
array(4) {
["entryID"]=>
string(2) "12"
["longitude"]=>
string(17) "-88.35108453430"
["latitude"]=>
string(16) "32.9298345948310"
["knownLocationTag"]=>
NULL
}
[13]=>
array(4) {
["entryID"]=>
string(2) "13"
["longitude"]=>
string(17) "-88.3513437005120"
["latitude"]=>
string(16) "32.9458453856350"
["knownLocationTag"]=>
NULL
}
[14]=>
array(4) {
["entryID"]=>
string(2) "14"
["longitude"]=>
string(17) "-88.3511544967700"
["latitude"]=>
string(16) "32.9293468765800"
["knownLocationTag"]=>
NULL
}
}
我想使用 PHP 遍历第二组数组中的每一个并运行一个函数,该函数包括来自两个数组的数据(在这种情况下,我们正在检查两个 GPS 位置之间的距离)以及它们是否在一个 x 英里范围内对于第一个数组中列表中的项目,它会增加“计数”值,或者如果它们超出该设置范围,则将该值添加到第一个数组中。
到目前为止,我的解决方案只有 4 个嵌套的 foreach 循环,而且越来越混乱。这是正确的方法还是有更好的方法?
foreach ($devices as $devicesKey => $devicesValue)
{
foreach ($devicesValue as $devicesKey2 => $devicesValue2)
{
$testarray[$devicesKey2] = $devicesValue2;
}
foreach ($locations as $locationsKey => $locationsValue)
{
foreach ($locationsValue as $locationsKey2 => $locationsValue2)
{
$testarray2[$locationsKey2] = $locationsValue2;
}
}
$test = distance($testarray['longitude'],$testarray['latitude'],$testarray2['longitude'],$testarray2['latitude']);
if ($test < .26)
{
//we would incriment the counter here since we match
}
else
{
//we would add the value to the array here since we are outside the bounds
}
}
【问题讨论】:
标签: php multidimensional-array