我有两个不同的阵列
$arr1=Array ( 'latitude' => 9.9252007, 'longitude' => 78.1197754, 'title' => 'Madurai' ) ;
$arr2=Array ( 'latitude' => 9.2323, 'longitude' => 78.23233, 'title' => 'Peraiyur' ) ;
我需要将两个数组合并为单个并需要通过json_encode转换为json,所以我的代码在这里合并数组并解析为json
$obj['markers']=(object) array( $arr1, $arr2 );
echo json_encode($obj);
输出如下
{"markers":
{"0":{"latitude":9.9252007,"longitude":78.1197754,"title":"Madurai"},
"1":{"latitude":9.2323,"longitude":78.23233,"title":"Peraiyur"}} }
但我需要它像跟随
{"markers":[ { "latitude":9.9252007, "longitude":78.1197754, "title":"Madurai" }, { "latitude":9.2323, "longitude":78.23233, "title":"Peraiyur" } ]}
如何删除它们?
答案 0 :(得分:1)
为什么要投入object
?如果你只是删除(object)
投射,它将会完成!
$obj['markers']=array( $arr1, $arr2 );
结果:
{"markers":[{"latitude":9.9252007,"longitude":78.1197754,"title":"Madurai"},{"latitude":9.2323,"longitude":78.23233,"title":"Peraiyur"}]}
答案 1 :(得分:1)
$obj['markers']=array( $arr1, $arr2 );
答案 2 :(得分:0)
这个对象:
{"markers":[
{ "latitude":9.9252007, "longitude":78.1197754, "title":"Madurai" },
{ "latitude":9.2323, "longitude":78.23233, "title":"Peraiyur" }
]}
与:
相同print_r(json_encode(array('markers' => array($arr1, $arr2))));
因此,无需进行任何额外的复杂编码。
答案 3 :(得分:0)
试试这个
$arr1=Array ( 'latitude' => 9.9252007, 'longitude' => 78.1197754, 'title' => 'Madurai' ) ;
$arr2=Array ( 'latitude' => 9.2323, 'longitude' => 78.23233, 'title' => 'Peraiyur' ) ;
$obj['markers']=array($arr1, $arr2);
echo json_encode($obj);
输出
{"markers":[{"latitude":9.9252007,"longitude":78.1197754,"title":"Madurai"},{"latitude":9.2323,"longitude":78.23233,"title":"Peraiyur"}]}
答案 4 :(得分:0)
这是必须的:
$obj['markers']=array(
array(
'latitude' => $entity->getLat(),
'longitude' => $entity->getLng(),
'title' => $entity->getTitle(),
)
);
$json = json_encode($obj);
$response = new Response($json);
$response->headers->set('Content-Type', 'application/json');
return $response;
它将生成这个json文件:
{"markers":[{"latitude":"36.80610237001900","longitude":"10.17517220741000","title":"Mon annonce"}]}