好的,所以我想根据另一个数组中的值为给定数组创建一个多维数组。
我有一个这样的数组:
"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")
)
我想将第一个数组拆分到第二个数组中给出的位置,基本上创建3个数组,其中第一个数组的值落在拆分值内。
所以输出应该是:
"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")
)
)
希望对某人有意义。
由于
答案 0 :(得分:1)
检查一下,
<?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>";
?>