duanpo8329 2017-05-16 04:09
浏览 16
已采纳

如何使用文件而不是数据库?

I have 2 arrays. The first array contains the correct word variants and the second array contains incorrect word variants. I want to write them by combining into one as an array, passing in the wrong version of the words to the key and to the value of the correct version of the word. Then write them to a file and use it. I saved the contents of the array in a file, but with a new record to the same file, all the data is cleared and only new entries are re-written and the old entries are deleted automatically. I want that before writing new data to an array, the array is checked for duplication and if such is not written in the file with the array, then let it be written to the file without clearing the previous data of the file. In general, that will select a file or database for storing more than a billion words. Does the database have the best speed or file?

Example first array:

$uncorrect = array
(
0 => "мувосокори",
1 => "мунаггас",
2 => "мангит",
3 => "мангития",
4 => "мунфачир",
5 => "мунфачира",
6 => "манфиатпарасти",
7 => "манфиатчу",
8 => "манфиатчуи",
9 => "манфиатхох",
10 => "манфи",
.....................
);

Example second array:

$correct = array
(
0 => "мувосокорӣ",
1 => "мунағғас",
2 => "манғит",
3 => "манғития",
4 => "мунфаҷир",
5 => "мунфаҷира",
6 => "манфиатпарастӣ",
7 => "манфиатҷӯ",
8 => "манфиатҷӯӣ",
9 => "манфиатхоҳ",
10 => "манфӣ",
.....................
);

I combined two arrays with this code:

$dict = array_combine($uncorrect, $correct);

Example result my array with data here:

$dict = array (
    "мувосокори" => "мувосокорӣ",
    "мунаггас" => "мунағғас",
    "мангит" => "манғит",
    "мангития" => "манғития",
    "мунфачир" => "мунфаҷир",
    "мунфачира" => "мунфаҷира",
    "манфиатпарасти" => "манфиатпарастӣ",
    "манфиатчу" => "манфиатҷӯ",
    "манфиатчуи" => "манфиатҷӯӣ",
    "манфиатхох" => "манфиатхоҳ",
    "манфи" => "манфӣ",
    "минкор" => "минқор",
    .....................................
    );

I am writing to a file with this code:

file_put_contents("data.json", json_encode($dict));

I will get the array with this code:

$array = json_decode(file_get_contents("data.json"));
  • 写回答

2条回答 默认 最新

  • dongraa1986 2017-05-16 04:18
    关注

    You would be better off using a database for this task.

    To solve your issue if you decide to keep with the file storage, the reason you are losing old entries is because you forgot to load them before adding new values.

    // more or less something like below
    $array = json_decode(file_get_contents("data.json"));
    $dict = array_combine($incorrect, $correct);
    $newArray = array_merge($array, $dict);
    file_put_contents("data.json", json_encode($newArray));
    

    This will not be efficient for billions or rows, especially if it's something that gets loaded every time a user loads a page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?