Php 将数组数据作为对象附加到json文件
我有一个简单的json文件,它有一个直接的图像链接和每个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
[
{
"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)我以为我已经这样做了,以确保它作为数组返回,但我意外地将bool参数添加到了文件中。但是仍然没有改变。文件内容('./images_list.json',json_encode($result));我在文件内容中添加了json_encode(result),现在收到错误:“array_merge():Argument#1不是数组”,尽管我通过添加可选的trueI将$data设置为数组而不是对象,但我现在收到了“array_merge:Argument#1不是数组”错误。我尝试了编辑的代码,但它仍然没有更改我的json文件。。我是否可以使用错误检查来查看问题所在?json文件与此代码也在同一目录中。太棒了,通过您的错误检查,我能够让它工作。不过,我注意到json文件有一点与之前有所不同:任何http://字符串都转换为http:\/\/(编码可以做到这一点吗?)。谢谢你的帮助和时间,我会调查逃生计划的。很高兴听到它对你有用。即使JSON中不需要正斜杠,也会转义正斜杠。查看此帖子以了解更详细的讨论。很高兴听到可以安全地忽略它:)我应该更新我的答案,因为我已经解决了这个问题。谢谢你的洞察力。
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);
?>