0

OK, so I would like to create a multi dimensional array for a given array based on the values in another array.

I have an array like this:

"coordinatesArray" => array(
    array("altitude"=>"88.9201049804688","dateAdded"=>"2016-10-19 09:04:17 +1000","latitude"=>"-37.787229","longitude"=>"144.940128","speed"=>"0.0"),
    array("altitude"=>"88.9201049804688","dateAdded"=>"2016-10-19 09:04:18 +1000","latitude"=>"-37.789705","longitude"=>"144.942424","speed"=>"0.0"),
    array("altitude"=>"88.9201049804688","dateAdded"=>"2016-10-19 09:04:19 +1000","latitude"=>"-37.792537","longitude"=>"144.946029","speed"=>"0.0"),
    array("altitude"=>"88.9201049804688","dateAdded"=>"2016-10-19 09:04:20 +1000","latitude"=>"-37.795487","longitude"=>"144.949891","speed"=>"0.0"),
    array("altitude"=>"88.9201049804688","dateAdded"=>"2016-10-19 09:04:21 +1000","latitude"=>"-37.797302","longitude"=>"144.951930","speed"=>"0.0"),
    array("altitude"=>"88.9201049804688","dateAdded"=>"2016-10-19 09:04:22 +1000","latitude"=>"-37.799319","longitude"=>"144.954419","speed"=>"0.0"),
    array("altitude"=>"88.9201049804688","dateAdded"=>"2016-10-19 09:04:23 +1000","latitude"=>"-37.800879","longitude"=>"144.956372","speed"=>"0.0")
)

and another array like this:

"coordinatesSplitMarkerArray" => array(
    array("latitude"=>"-37.789705","longitude"=>"144.942424"),
    array("latitude"=>"-37.799319","longitude"=>"144.954419")
)

I would like to split the first array at the positions given in the second array essentially creating 3 arrays with the values from the first array that fall within the split values.

So the output should be:

"coordinatesArray" => array(
    array(
        array("altitude"=>"88.9201049804688","dateAdded"=>"2016-10-19 09:04:17 +1000","latitude"=>"-37.787229","longitude"=>"144.940128","speed"=>"0.0"),
    ),
    array(
        array("altitude"=>"88.9201049804688","dateAdded"=>"2016-10-19 09:04:18 +1000","latitude"=>"-37.789705","longitude"=>"144.942424","speed"=>"0.0"),
        array("altitude"=>"88.9201049804688","dateAdded"=>"2016-10-19 09:04:19 +1000","latitude"=>"-37.792537","longitude"=>"144.946029","speed"=>"0.0"),
        array("altitude"=>"88.9201049804688","dateAdded"=>"2016-10-19 09:04:20 +1000","latitude"=>"-37.795487","longitude"=>"144.949891","speed"=>"0.0"),
        array("altitude"=>"88.9201049804688","dateAdded"=>"2016-10-19 09:04:21 +1000","latitude"=>"-37.797302","longitude"=>"144.951930","speed"=>"0.0"),
    ),
    array(
        array("altitude"=>"88.9201049804688","dateAdded"=>"2016-10-19 09:04:22 +1000","latitude"=>"-37.799319","longitude"=>"144.954419","speed"=>"0.0"),
        array("altitude"=>"88.9201049804688","dateAdded"=>"2016-10-19 09:04:23 +1000","latitude"=>"-37.800879","longitude"=>"144.956372","speed"=>"0.0")
    )
)

Hope that makes sense to someone.

Thanks

3
  • 6
    did you tried anything? yes? then show us Commented Oct 24, 2016 at 4:37
  • Just found a much easier way. Instead of storing a corresponding lat/lng values I will just store the position at which the array should be split. Thanks for looking though. Commented Oct 24, 2016 at 4:51
  • Great to hear that.Sounds good! Commented Oct 24, 2016 at 4:57

1 Answer 1

1

Check this,

<?php
$coordinatesArray = array(
    array("altitude"=>"88.9201049804688","dateAdded"=>"2016-10-19 09:04:17 +1000","latitude"=>"-37.787229","longitude"=>"144.940128","speed"=>"0.0"),
    array("altitude"=>"88.9201049804688","dateAdded"=>"2016-10-19 09:04:18 +1000","latitude"=>"-37.789705","longitude"=>"144.942424","speed"=>"0.0"),
    array("altitude"=>"88.9201049804688","dateAdded"=>"2016-10-19 09:04:19 +1000","latitude"=>"-37.792537","longitude"=>"144.946029","speed"=>"0.0"),
    array("altitude"=>"88.9201049804688","dateAdded"=>"2016-10-19 09:04:20 +1000","latitude"=>"-37.795487","longitude"=>"144.949891","speed"=>"0.0"),
    array("altitude"=>"88.9201049804688","dateAdded"=>"2016-10-19 09:04:21 +1000","latitude"=>"-37.797302","longitude"=>"144.951930","speed"=>"0.0"),
    array("altitude"=>"88.9201049804688","dateAdded"=>"2016-10-19 09:04:22 +1000","latitude"=>"-37.799319","longitude"=>"144.954419","speed"=>"0.0"),
    array("altitude"=>"88.9201049804688","dateAdded"=>"2016-10-19 09:04:23 +1000","latitude"=>"-37.800879","longitude"=>"144.956372","speed"=>"0.0")
);


$coordinatesSplitMarkerArray = array(
    array("latitude"=>"-37.789705","longitude"=>"144.942424"),
    array("latitude"=>"-37.799319","longitude"=>"144.954419")
);

$data = array();
$new = 0;
$splitMarker = 0;
foreach($coordinatesArray as $i=>$value){
    if($coordinatesSplitMarkerArray[$splitMarker]['latitude'] == $coordinatesArray[$i]['latitude'] && $coordinatesSplitMarkerArray[$splitMarker]['longitude'] == $coordinatesArray[$i]['longitude']){
        $new++;
        if(count($coordinatesSplitMarkerArray)-1>$splitMarker){
            $splitMarker++;
        }
    }
    $data[$new][] = $coordinatesArray[$i];
}

echo "<pre>";
print_r($data);
echo "</pre>";
?>
Sign up to request clarification or add additional context in comments.

3 Comments

good logic. +1 from my side . just one correction $i<count($coordinatesArray); take the count outside of the loop (count it first and assign it to a variable and use that variable into loop). That will be more eligent
Thank You, I changed the for loop for foreach.
even more accurate and sound logic. Good

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.