Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP数组到JSON-需要数组形成帮助_Php_Arrays_Json_Google Maps Api 3_Multidimensional Array - Fatal编程技术网

PHP数组到JSON-需要数组形成帮助

PHP数组到JSON-需要数组形成帮助,php,arrays,json,google-maps-api-3,multidimensional-array,Php,Arrays,Json,Google Maps Api 3,Multidimensional Array,我正在尝试为GoogleMapsAPI创建一个json提要 我可以生成的JSON提要是: [{"latitude":"40.651602","longitude":"-111.918734","title":"64439","content":"test"}][{"latitude":"41.330500","longitude":"-111.851600","title":"80283","content":"test"}][{"latitude":"40.624500","longitud

我正在尝试为GoogleMapsAPI创建一个json提要

我可以生成的JSON提要是:

 [{"latitude":"40.651602","longitude":"-111.918734","title":"64439","content":"test"}][{"latitude":"41.330500","longitude":"-111.851600","title":"80283","content":"test"}][{"latitude":"40.624500","longitude":"-111.976500","title":"95964","content":"test"}][{"latitude":"40.715020","longitude":"-111.494717","title":"96995","content":"test"}][{"latitude":"40.664200","longitude":"-111.843295","title":"101724","content":"test"}][{"latitude":"40.513917","longitude":"-111.403612","title":"245697","content":"test"}][{"latitude":"40.509000","longitude":"-111.419919","title":"245716","content":"test"}][{"latitude":"40.473700","longitude":"-111.414499","title":"245742","content":"test"}][{"latitude":"40.509943","longitude":"-111.402999","title":"245792","content":"test"}][{"latitude":"40.512200","longitude":"-111.477263","title":"245874","content":"test"}][{"latitude":"40.526623","longitude":"-111.475199","title":"245904","content":"test"}][{"latitude":"40.530400","longitude":"-111.487500","title":"245905","content":"test"}][{"latitude":"40.526619","longitude":"-111.473531","title":"245921","content":"test"}][{"latitude":"40.517013","longitude":"-111.487827","title":"245926","content":"test"}][{"latitude":"40.519500","longitude":"-111.497780","title":"245940","content":"test"}][{"latitude":"40.511981","longitude":"-111.409799","title":"245944","content":"test"}][{"latitude":"39.373348","longitude":"-111.574996","title":"245946","content":"test"}][{"latitude":"40.514900","longitude":"-111.402590","title":"245982","content":"test"}][{"latitude":"40.513700","longitude":"-111.467712","title":"246093","content":"test"}][{"latitude":"40.515600","longitude":"-111.398192","title":"246124","content":"test"}]
生成此提要的代码是:

while($data = $do->FetchRow($search)) {
    //print_r($data);
    $a = array();
    $a[] = array("latitude" => $data['latitude'], "longitude" => $data['longitude'], "title" => $data['listno'], "content" => "test");

    echo json_encode($a);
    //echo json_encode(array("markers" => $a), JSON_UNESCAPED_SLASHES);
}
我需要的是生成如下所示的数组:

var json = [{"title":"Helgelandskysten","longitude":"12.63376","latitude":"66.02219"},{"title":"Tysfjord","longitude":"16.50279","latitude":"68.03515"},{"title":"Sledehunds-ekspedisjon","longitude":"7.53744","latitude":"60.08929"},{"title":"Amundsens sydpolferd","longitude":"11.38411","latitude":"62.57481"},{"title":"Vikingtokt","longitude":"6.96781","latitude":"60.96335"},{"title":"Tungtvann- sabotasjen","longitude":"8.49139","latitude":"59.87111"}];
我尝试过使用阵列合并、阵列推送等等。我被难住了,有人想要一个好的拼图吗

只是:

echo json_encode(array("latitude" => $data['latitude'], "longitude" => $data['longitude'], "title" => $data['listno'], "content" => "test"));

因为我想你只有一排,所以这将是足够的。

好的,你在做什么有几个问题。首先,您为每个单独的行回显一个json编码的字符串,因为您在循环中调用它。这就是为什么在生成的字符串中有几个[]数组

您需要做的是:

$a = array();
while($data = $do->FetchRow($search)) {
   $a[] = array("latitude" => $data['latitude'], "longitude" => $data['longitude'], "title" => $data['listno']);
}
echo json_encode($a);
而且,你得到的是一个数字,而不是你想要的标题。我无法直接帮助您,但您正在使用:

$data['listno']

作为标题,我假设它实际上代表列表编号。您很可能需要使用字段标题(如果存在)或修改搜索查询。

如果我这样做,它会告诉我:这修复了数组!非常感谢。有趣的是,谷歌地图并没有填充lol。代码可以在别人的服务器上运行,但在我的服务器上却不起作用。有趣的是,如果看不到更多的代码,就很难做出真正的响应。如果这种情况仍然存在,您应该提出另一个问题。我很高兴你的阵列问题得到了解决。不用担心。如果我需要地图方面的帮助,我会提出一个新的问题,因为你已经正确地回答了这个问题,我会觉得很抱歉在这个问题上附加更多!