【问题标题】:Writing json to file, only one line written将json写入文件,只写了一行
【发布时间】:2020-03-15 16:41:22
【问题描述】:

我有这段代码用于将 json 写入文件

<?php
require_once 'vendor/autoload.php';
$faker = Faker\Factory::create();

function getAge($start = 18, $end = 100, $repeat = 20){
    $result = null;
    static $ages = array();
    if ( empty($ages) ) {
        for ($i= $start; $i <= $end; $i++) {
            for($j = 0; $j < $repeat; $j++)
            $ages[] = $i;
        }
    }
    $index = rand(0, count($ages));
    $result = $ages[ $index ];
    unset($ages[ $index ]);
    $ages = array_values($ages);

    return $result;
}

$superstars = array("Adam Cole","Finn Balor","Pete Dunne","Jordan Devlin","Noam Dar");
$fan_favourite_superstar_name = $superstars[ mt_rand( 0, count($superstars) -1 ) ];

$cities = array("London","Manchester","Leeds","Bristol");
$fan_location = $cities[ mt_rand( 0, count($cities) -1 ) ];

$the_models = array("Iphone","Nokia","Huawei","Samsung");
$fan_phone_model = $the_models[ mt_rand( 0, count($the_models) -1 ) ];

for ($x = 1; $x <= 100; $x++) {
    echo $x;
$array = Array (
    "$x" => Array (
        "id" => uniqid(),
        "fan_favourite_superstar_name" => $fan_favourite_superstar_name,
        "fan_location" => $fan_location,
        "fan_phone_model" => $fan_phone_model,
        "fan_name" => $faker->name,
        "fan_age" => getAge(),
        "fan_comments" => $faker->text,
        "fan_picture" => rand(1,500),
        "last_updated" => time() + rand(1,1000),
    )
);

// encode array to json
$json = json_encode(array('fans' => $array));
//write json to file
if (file_put_contents("data.json", $json))
    echo "JSON file created successfully..."."<br/>";
else 
    echo "Oops! Error creating json file...";
}
?>

当我运行时,只写第一行。为什么其他行不写了?

【问题讨论】:

  • 不确定是否在循环中正确设置了数组中的值,试试$array[$x] = Array ( "id" =&gt; uniqid(),
  • 您每次都在覆盖文件。见this
  • @El_Vanja 你是对的,这是缺少if ( file_put_contents('data.json', $json.PHP_EOL , FILE_APPEND | LOCK_EX))
  • 如果您将单独的 JSON 编码字符串附加到文件中,则它不再是 JSON 文件。
  • @NigelRen 是的。我想要我的 json {"fans":[{"id":"5e6e601da9a44","fan_favourite_superstar_name":"Pete Dunne","fan_location":"Bristol","fan_gender":"Female","fan_phone_model":"Nokia","fan_name":"Shemar Oberbrunner","fan_age":55,"fan_comments":"Et ","fan_picture":189,"last_updated":1584292832},{"id":"5e6e601da9cc1","fan_favourite_superstar_name":"Pete Dunne","fan_location":"Bristol","fan_gender":"Female","fan_phone_model":"Nokia","fan_name":"Gertrude Wiegand","fan_age":45,"fan_comments":"Repudiandae ","fan_picture":346,"last_updated":1584292326}]} 格式

标签: php


【解决方案1】:

您将在循环的每次迭代中覆盖文件。你需要告诉file_put_contents你想要附加的函数,像这样:

file_put_contents("data.json", $json, FILE_APPEND)

编辑:

尽管这回答了发布的问题,正如 Nigel Ren 指出的那样,这不会创建有效的 JSON 文件。关于您对所需格式的后续评论,您需要在循环内构建数组,然后再编写一次(通过将 $json = json_encode(array('fans' =&gt; $array)); 之后的所有内容移出循环)。

构建它的正确方法是遵循奈杰尔关于赋值的第一条评论(同时删除的其他答案也处理过)以避免每次都覆盖数组:

$array["$x"] = Array (
    "id" => uniqid(),
    ...

因此,最终,您甚至不需要附加标志。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-22
    • 2015-11-14
    • 1970-01-01
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    相关资源
    最近更新 更多