将数组数据作为对象附加到json文件
我有一个简单的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代码w / comments:
$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_decode解码为数组应该很简单,将新数组合并到该数组上,然后对结果进行编码并放入同一个文件中。 我一直无法捕捉任何类型的错误。
$data = json_decode($file, true);
//When TRUE, returned objects will be converted into associative arrays.
unset($file);
$newImage = array('image' => $link, 'folder' => $category);
//$newImage['image'] = $link;
//$newImage['folder'] = $category;
$result = array_merge($data, $newImage);
//don't need to cast
参考PHP json_decode手册
编辑下面的代码工程(测试)
function addToJsonImageList($link, $category) {
$file = file_get_contents('./images_list.json', true);
$data = json_decode($file,true);
unset($file);
//you need to add new data as next index of data.
$data[] = array('image' => $link, 'folder' => $category);
$result=json_encode($data);
file_put_contents('./images_list.json', $result);
unset($result);
}
编辑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 beforen";
var_dump ($data);
//you need to add new data as next index of data.
$data[] = array('image' => $link, 'folder' => $category);
echo "data aftern";
var_dump ($data);
$result=json_encode($data);
if (file_put_contents('./images_list.json', $result) === false){
die('unable to write file');
}
unset($result);
}
?>
比方说,你有这个名为(playerJson)的.json文件
{
"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(myPhpFile.php)中必须做的是:(我想你是从表单加载你的数据)
<?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);
?>
就这样 ! 你在“玩家”对象中有一个新行。 但是如果你想添加一个新对象,请避免array_push函数中的$ json_data之后的[Players]。
你可以做的是解码json然后合并它们。
$data = json_decode(file_get_contents(./images_list.json));
$result = array_merge((array)$data, (array)$newImage);
然后输出json_encode
file_put_contents('./images_list.json', json_encode($result, JSON_FORCE_OBJECT));
链接地址: http://www.djcxy.com/p/47933.html
上一篇: Append array data as objects to json file
下一篇: how to write a javascript variable as source for img tag in html