我已经编辑了该问题以解决问题,因为问题不是我所想的。当我创建数组时,我正在做:
$points[] = array("location" => array( $markers[$j]['latitude'] , $markers[$j]['longitude'] ) );
出于某种原因它想要:
$points[] = array("location" => array( floatvar($markers[$j]['latitude']) , floatvar($markers[$j]['longitude']) ) );
我不太确定为什么会这样,但这似乎已经为我完成了工作。因此,最终代码如下:
for ($j = 0; $j < count($markers); $j++) {
$points[] = array("location" => array( $markers[$j]['latitude'] , $markers[$j]['longitude'] ) );
}
特别感谢@ D.Schaller帮助我完成了对聊天的调试:)
答案 0 :(得分:1)
您可以使用array_intersect_key从数组中选择与$ match键匹配的项目。
// Create placeholder array to match against
$match = ['longitude' => "", 'latitude' => ""];
foreach($_POST['markers'] as $sub){
$new[] = array_intersect_key($sub, $match);
}
var_dump($new);
输出:
array(10) {
[0]=>
array(2) {
["latitude"]=>
string(11) "45.26429390"
["longitude"]=>
string(11) "-0.91037940"
}
[1]=>
array(2) {
["latitude"]=>
string(19) "44.8247082823002360"
["longitude"]=>
string(20) "-0.58459281921386720"
}
[2]=>
array(2) {
["latitude"]=>
string(10) "44.6015560"
["longitude"]=>
string(20) "-1.08606009999994060"
}
[3]=>
array(2) {
["latitude"]=>
string(10) "44.7300940"
["longitude"]=>
string(20) "-1.04289489999996470"
}
[4]=>
array(2) {
["longitude"]=>
string(20) "-1.17691170000000510"
["latitude"]=>
string(10) "44.6437140"
}
[5]=>
array(2) {
["latitude"]=>
string(18) "45.068182578090370"
["longitude"]=>
string(20) "-1.14553928375244140"
}
[6]=>
array(2) {
["longitude"]=>
string(15) "-0.593261718750"
["latitude"]=>
string(18) "44.331707186809210"
}
[7]=>
array(2) {
["latitude"]=>
string(18) "44.721281586556690"
["longitude"]=>
string(21) "-0.453948412793351960"
}
[8]=>
array(2) {
["latitude"]=>
string(18) "44.833484299999990"
["longitude"]=>
string(20) "-0.56840629999999240"
}
[9]=>
array(2) {
["longitude"]=>
string(20) "-0.58094501495361330"
["latitude"]=>
string(18) "45.144727399384470"
}
}