Fun with Array

I have a locationlist class which has geographical latitude and longitude as shown below


<?php 
namespace App\LocationProviders; 
class LocationList 
{ 
public function getLocations() 
{ 
       $locationCoords = []; $locationCoords[0] = [ ["lat" => 43.75812569798546, "lng" => -79.79318417608738],
            ["lat" => 43.758138774543475, "lng" => -79.7931607067585],
            ["lat" => 43.75810390371577, "lng" => -79.7931244969368],
            ["lat" => 43.75809034283284, "lng" => -79.79314729571342]
        ];

        $locationCoords[1] = [
            ["lat" => 43.7581397431773, "lng" => -79.79315668344498],
            ["lat" => 43.758152819732246, "lng" => -79.7931332141161],
            ["lat" => 43.75811843322978, "lng" => -79.7930970042944],
            ["lat" => 43.758105356667315, "lng" => -79.79311980307102]
        ];
        $locationCoords[2] = [
            ["lat" => 43.7581547569994, "lng" => -79.79312919080257],
            ["lat" => 43.7581673492344, "lng" => -79.7931070625782],
            ["lat" => 43.758132962740284, "lng" => -79.79307018220425],
            ["lat" => 43.75812037049805, "lng" => -79.79309231042862]
        ];

    return $locationCoords;
     }

}

Now what I intend to do is, I want to have array of latitude and array of longitude for the array $locationCoords[0],$locationCoords[1],$locationCoords[2] and so on.For this i have written below code


public function vehicle_map_with_info()
    {

      $locationListInfo = new LocationList();

        $locationinfo = $locationListInfo->getLocations();

        $newCoordinateArr = [];
        foreach ($locationinfo as $key => $value) {
            $latitudeArr = [];
            $longitudeArr = [];
            foreach ($value as $keyanother => $anothervalue) {

                $latitudeArr[] = $anothervalue['lat'];
                $longitudeArr[] = $anothervalue['lng'];
            }
            $newCoordinateArr[$key]['lat'] = $latitudeArr;
            $newCoordinateArr[$key]['lng'] = $longitudeArr;


        }

        echo "<pre>";
        //print_r($latitudeArr);
        return $newCoordinateArr;

}

Here it is, now you can get array in the form as shown below:


[
{
"lat": [43.75812569798546,43.758138774543475,43.75810390371577,43.75809034283284],
"lng": [-79.79318417608738,-79.7931607067585,-79.7931244969368,-79.79314729571342]
},
{
"lat": [43.7581397431773,43.758152819732246,43.75811843322978,43.758105356667315],
"lng": [-79.79315668344498,-79.7931332141161,-79.7930970042944,-79.79311980307102]
}
]

Fun with Array -2

Now i have another requirement too, that is i want array of latitude and longitude in the form of

$finalcoordinates=array("lat1 lng1","lat2 lng2","lat3 lng3"); ie

$finalcoordinates = array("43.7592439755264 -79.79324888437986","43.759231383517985 -79.79323044419289","43.759203535797674 -79.79327369481325","43.759215643503765 -79.7932904586196","43.7592439755264 -79.79324888437986");

In order to get this output i have written a code as shown below:


public function vehicle_map_with_info($vin_id)
    {
        $locationObj = new FindLocation();
        $locationinfo = $locationObj->showLocations();

        foreach ($locationinfo as $key => $value) {
            $newCoordinateArr = [];
            foreach ($value as $seckey => $finalvalue) {
                $newCoordinateArr[$seckey] = $finalvalue['lat'] . " " . $finalvalue['lng'];

            }
          
          
        }

        return $newCoordinateArr;
}

Now you get your output as


[
"43.758909802097 -79.794197380543",
"43.758935954868 -79.794150441885",
"43.758917551068 -79.794130325317",
"43.758891398289 -79.794177263975",
"43.758909802097 -79.794197380543"
]

Hope it gives solution to your problem.

Advertisements