Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.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 如何将2个JSON文件合并到一个JSON FeatureCollection中_Php_Json_Merge_Geojson - Fatal编程技术网

Php 如何将2个JSON文件合并到一个JSON FeatureCollection中

Php 如何将2个JSON文件合并到一个JSON FeatureCollection中,php,json,merge,geojson,Php,Json,Merge,Geojson,我有两个JSON文件,每个文件都包含一个FeatureCollection,其结构相同,但数据不同。我试图将它们合并到一个JSON文件中,作为包含所有数据的单个FeatureCollection。我几乎做到了这一点,但在文件的开头重复了“FeatureCollection”,使其成为无效的JSON 我认为这与我对两个文件分别进行JSON编码的方式有关,然后在组合它们时再次进行JSON编码,但在尝试了一天的各种组合后,我还没有找到答案 这是创建第一个JSON文件的代码(其中$results由数据库

我有两个JSON文件,每个文件都包含一个FeatureCollection,其结构相同,但数据不同。我试图将它们合并到一个JSON文件中,作为包含所有数据的单个FeatureCollection。我几乎做到了这一点,但在文件的开头重复了“FeatureCollection”,使其成为无效的JSON

我认为这与我对两个文件分别进行JSON编码的方式有关,然后在组合它们时再次进行JSON编码,但在尝试了一天的各种组合后,我还没有找到答案

这是创建第一个JSON文件的代码(其中$results由数据库查询生成):

第二个文件(file2.json)以相同的方式创建,例如:

$geojson = array( 'type' => 'FeatureCollection', 'features' => array());

while ( $results->fetch() ) {
    $feature = array(
        'type' => 'Feature', 
              'properties' => array(
            'name' => $results->field('name')
            ),
      'geometry' => array(
        'type' => 'Point',
        'coordinates' => array((float)$results->field('long'), (float)$results->field('lat'))
            )
        );
    array_push($geojson['features'], $feature);
};

// // Create JSON file
    $fp = fopen('file2.json', 'w');
fwrite($fp, json_encode($geojson));
fclose($fp);
然后,我使用以下代码将它们组合在一起:

    $jsonString = file_get_contents('file2.json');
    $jsonString2 = file_get_contents('file1.json');   
    $data = json_decode($jsonString, true);
    $data2 = json_decode($jsonString2, true);

    $op = array_merge_recursive( $data, $data2 );

    $fp = fopen('file3.json', 'w');
    fwrite($fp, json_encode($op));
    fclose($fp); 
生成的文件基本上没有问题,它包含了我需要的所有数据,并且格式正确,除了在文件开头有以下内容之外:

{"type":["FeatureCollection","FeatureCollection"],"features":[{"type":"Feature","properties":{"name":"......etc
而不是:

{"type":["FeatureCollection"],"features":[{"type":"Feature","properties":{"name":"......etc

我不明白为什么在开始时有两个“FeatureCollection”实例,或者如何只生成一个。

当您合并两组数据时,这两组数据都有自己的“type”副本,合并将创建一个包含这两个项的输出

考虑一下

$data = [ "type" => "FeatureCollection"];
$data2 = [ "type" => "FeatureCollection"];

$op = array_merge_recursive( $data, $data2 );

print_r($op);
这将输出

Array
(
    [type] => Array
        (
            [0] => FeatureCollection
            [1] => FeatureCollection
        )

)
因为这是两个源阵列的组合

解决这个问题的一个简单方法是重新设置数组中的值,这段代码只选择第一个值并将其设置为

$op["type"] = $op["type"][0];
print_r($op);
将给予

Array
(
    [type] => FeatureCollection
)

太好了,非常感谢,这正是我需要的。
Array
(
    [type] => FeatureCollection
)