Php 将数组数据作为对象附加到json文件

Php 将数组数据作为对象附加到json文件,php,json,file,Php,Json,File,我有一个简单的json文件,它有一个直接的图像链接和每个json对象的文件夹名: [ { "image": "http://placehold.it/350x150", "folder": "Small" }, { "image": "http://placehold.it/450x250", "folder": "Med

我有一个简单的json文件,它有一个直接的图像链接和每个json对象的文件夹名:

[
        {
                "image": "http://placehold.it/350x150",
                "folder": "Small"
        },
        {
                "image": "http://placehold.it/450x250",
                "folder": "Medium"
        },
        {
                "image": "http://placehold.it/550x350",
                "folder": "Medium"
        }
]
我想从一篇文章中附加这两个值,但结果是一个未更改的json文件。以下是带有注释的PHP代码:

$directLink = $_POST['txtDirectLink'];
$category = $_POST['selectCategoriesImages'];
$util->addToJsonImageList($directLink, $category);

//decodes the json image list, merges to the decoded array a new image, then encodes back to a json file
function addToJsonImageList($link, $category) {
    $file = file_get_contents('./images_list.json', true);
    $data = json_decode($file);
    unset($file);

    $newImage = array('image' => $link, 'folder' => $category);
    //$newImage['image'] = $link;
    //$newImage['folder'] = $category;
    $result = array_merge((array)$data, (array)$newImage);

    json_encode($result);
    file_put_contents('./images_list.json', $result);
    unset($result);
}

逻辑是,将文件作为数组进行json_解码,将新数组合并到该数组中,然后对结果进行编码并放入同一个文件中应该很简单。我也无法捕获任何类型的错误。

您可以做的是解码json,然后合并它们

$data = json_decode(file_get_contents(./images_list.json));

$result = array_merge((array)$data, (array)$newImage);
然后只需输出
json\u encode

file_put_contents('./images_list.json', json_encode($result, JSON_FORCE_OBJECT));
参考PHP手册

编辑以下代码(已测试)

编辑2添加了大量错误报告和调试。请让我知道以下的输出。下面的代码未经测试(仅在此处键入)。如果发现任何语法错误,请进行修复。这里很晚了,我只能明天查我的时间,但我可以回复

<?php
//display all errors and warnings
error_reporting(-1);
addToJsonImageList('test link', 'test category');

function addToJsonImageList($link, $category) {
    $file = file_get_contents('./images_list.json', true);
    if ($file===false)die('unable to read file');
    $data = json_decode($file,true);
    if ($data ===NULL)die('Unable to decode');
    unset($file);
    echo "data before\n";
    var_dump ($data);
    //you need to add new data as next index of data.
    $data[] = array('image' => $link, 'folder' => $category);
    echo "data after\n";
    var_dump ($data);
    $result=json_encode($data);
    if (file_put_contents('./images_list.json', $result) === false){
        die('unable to write file');
    }
    unset($result);
}
?>

您的问题在于以下代码行

json_encode($result);
file_put_contents('./images_list.json', $result);
您没有捕获
json_encode
的结果,因此您需要在文件中写入
数组

您需要将代码调整为如下所示

$result = json_encode($result);
file_put_contents('./images_list.json', $result);

假设您有一个名为(playerJson)的.json文件

现在我们在php(myPhpFile.php)中要做的是:(我假设您正在从表单加载数据)


就这些!在“玩家”对象中有一个新行。
但是如果你想添加一个新的对象,在array\u push函数中避免在$json\u data之后使用[Players]。

试试
$data=json\u decode($file,true)json_encode($result);
file_put_contents('./images_list.json', $result);
$result = json_encode($result);
file_put_contents('./images_list.json', $result);
{

"Players":
   [
     {"Name":"Arun","Arm":"Gun","num":"1"},
     {"Name":"sssv","Arm":"Arc","num":"2"},
     {"Name":"Surya","Arm":"Bomb","num":"3"},
     {"Name":"sssv","Arm":"Fire","num":"4"}

   ]
}
<?php

$json = file_get_contents('playerJson.json');
$json_data = json_decode($json,true);

$newar = array(
           'Name'=>$_POST['nameField'] ,
           'Arm' =>$_POST['armField'],
           'Num' =>$_POST['numField']
);
//saving data in Players object...
array_push($json_data['Players'], $newar);

$json = json_encode($json_data);

file_put_contents('playerJson.json', $json);

?>