简体   繁体   English

合并json文件和php数组

[英]merge json file and a php array

Hi all I have a json file like this: 大家好,我有一个像这样的json文件:

[
   {
      "search":1,
      "hotelId":"YYB",
      "combination":"0|1|0|0|0|0",
   },
   {
      "search":1,
      "hotelId":"YYB",
      "combination":"0|1|0|0|0|0",
   },
   {
      "search":1,
      "hotelId":"YYW",
      "combination":"0|1|0|0|0|0",
   }
]

And I want to add to this json an array php converted into a json. 我想向这个json添加一个数组php,转换为json。

This is my php array 这是我的PHP数组

array(1) {
  [0]=>
  array(24) {
    ["search"]=>
    int(1)
    ["hotelId"]=>
    string(3) "rrr"
    ["combination"]=>
    string(11) "0|1|0|0|0|0"

  }
  [1]=>
  array(24) {
    ["search"]=>
    int(1)
    ["hotelId"]=>
    string(3) "ttt"
    ["combination"]=>
    string(11) "0|1|0|0|0|0"
}

I'm trying to add my encode php array to the json file. 我试图将我的编码php数组添加到json文件。

This is what I have tried: 这是我尝试过的:

$filename = 'json_upload/rooms.json';
$result = fread($file2, filesize($filename));
$arr = $result;
$arr_ret_room = $room_arr; //my php array
$res = array_merge_recursive((array)$arr, (array)$arr_ret_room);
fwrite($file2,  json_encode($res));
fclose($file2);

I have also tried with array_merge the result doesn't change when I open the json file the script add me a new root element like this: 我也尝试过用array_merge打开json文件时结果不会改变,脚本为我添加了一个新的根元素,如下所示:

[
       {
          "search":1,
          "hotelId":"YYB",
          "combination":"0|1|0|0|0|0",
       },
       {
          "search":1,
          "hotelId":"YYB",
          "combination":"0|1|0|0|0|0",
       },
       {
          "search":1,
          "hotelId":"YYW",
          "combination":"0|1|0|0|0|0",
       }
    ]
[
       {
          "search":1,
          "hotelId":"rrr",
          "combination":"0|1|0|0|0|0",
       },
       {
          "search":1,
          "hotelId":"ttt",
          "combination":"0|1|0|0|0|0",
       }
    ]

Instead of this: 代替这个:

[
           {
              "search":1,
              "hotelId":"YYB",
              "combination":"0|1|0|0|0|0",
           },
           {
              "search":1,
              "hotelId":"YYB",
              "combination":"0|1|0|0|0|0",
           },
           {
              "search":1,
              "hotelId":"YYW",
              "combination":"0|1|0|0|0|0",
           },
           {
              "search":1,
              "hotelId":"rrr",
              "combination":"0|1|0|0|0|0",
           },
           {
              "search":1,
              "hotelId":"ttt",
              "combination":"0|1|0|0|0|0",
           }
        ]

How can I merge correctly? 如何正确合并?

Thanks 谢谢

If I understand you correct: 如果我了解您正确的话:

// assuming that $file contains contents of json file

$arr = array(...); //your php array
$jsonArr = json_decode($file);

$result = $arr + $jsonArr;

then you can save your $result to your file. 那么您可以将$result保存到文件中。

It's better to convert JSON string to array then merge them instead of combining these JSON string. 最好将JSON字符串转换为数组,然后合并它们,而不是合并这些JSON字符串。 You need to change this: 您需要更改此:

    $arr = $result;
    $arr_ret_room = $room_arr; //my php array
    $res = array_merge_recursive((array)$arr, (array)$arr_ret_room);
    fwrite($file2,  json_encode($res));
    fclose($file2);

to

    $array1 = json_decode($result,true);
    $array2 = $arr_ret_room;
    $newArray = array_merge($array1,$array2);
    $newJSON = json_encode($newArray);
    fwrite($file2,  $newJSON); 
    fclose($file2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM