繁体   English   中英

合并json文件和php数组

[英]merge json file and a php array

大家好,我有一个像这样的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",
   }
]

我想向这个json添加一个数组php,转换为json。

这是我的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"
}

我试图将我的编码php数组添加到json文件。

这是我尝试过的:

$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);

我也尝试过用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",
       }
    ]

代替这个:

[
           {
              "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",
           }
        ]

如何正确合并?

谢谢

如果我了解您正确的话:

// assuming that $file contains contents of json file

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

$result = $arr + $jsonArr;

那么您可以将$result保存到文件中。

最好将JSON字符串转换为数组,然后合并它们,而不是合并这些JSON字符串。 您需要更改此:

    $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);

    $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