PHP 自动相互计算多维数组
PHP auto compute multidimensional arrays against one another
我有一个多维数组。
$parent_array($child1('lat','long'),$child2('lat','long')...)
每个请求的子数组数量可以是从 2 开始的任何值。
然后将父数组调用到一个函数中,该函数在一个或多个点之间具有 google 距离计算器。
function distance($parent_array)
{
foreach ( $parent_array as $child){
$distance = file_get_contents('https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins='.$child1['lat'].','.$child1['long'].'&destinations='.$child2['lat'].','.$child2['long'].'&key=********************************');
$trim = trim($this->objectToArray($distance)['rows'][0]['elements'][0]['distance']['text'],' km');
}
return $trim;
}
我需要使用
获取此函数来计算所有已发布的子数组之间的距离,如上述函数所示
$child1 and $child2
据我了解,您需要的是笛卡尔积。这就像 2 foreach
个周期一样简单:
foreach ($points as $point1) {
foreach ($points as $point2) {
computeDistance($point1, $point2);
}
}
我有一个多维数组。
$parent_array($child1('lat','long'),$child2('lat','long')...)
每个请求的子数组数量可以是从 2 开始的任何值。 然后将父数组调用到一个函数中,该函数在一个或多个点之间具有 google 距离计算器。
function distance($parent_array)
{
foreach ( $parent_array as $child){
$distance = file_get_contents('https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins='.$child1['lat'].','.$child1['long'].'&destinations='.$child2['lat'].','.$child2['long'].'&key=********************************');
$trim = trim($this->objectToArray($distance)['rows'][0]['elements'][0]['distance']['text'],' km');
}
return $trim;
}
我需要使用
获取此函数来计算所有已发布的子数组之间的距离,如上述函数所示$child1 and $child2
据我了解,您需要的是笛卡尔积。这就像 2 foreach
个周期一样简单:
foreach ($points as $point1) {
foreach ($points as $point2) {
computeDistance($point1, $point2);
}
}