-3

I have a multidimensional array.

$parent_array($child1('lat','long'),$child2('lat','long')...)

The amount of child arrays can be of any value per request starting from 2. The parent array is then called into a function that has a google distance calculator between to or more points.

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;
}

I need to get this function to calculate the distance between all posted child arrays as illustrated on the above function using

$child1 and $child2

s1nb2
  • 82
  • 1
  • 7

1 Answers1

1

As I understand you, what you need is a cartesian product. This is as simple as 2 foreach cycles:

foreach ($points as $point1) {
    foreach ($points as $point2) {
        computeDistance($point1, $point2);
    }
}
q200996
  • 26
  • 1