小编ben*_*bob的帖子

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

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

[
        {
                "image": "http://placehold.it/350x150",
                "folder": "Small"
        },
        {
                "image": "http://placehold.it/450x250",
                "folder": "Medium"
        },
        {
                "image": "http://placehold.it/550x350",
                "folder": "Medium"
        }
]
Run Code Online (Sandbox Code Playgroud)

我想从帖子中追加这两个值,但结果是一个未更改的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); …
Run Code Online (Sandbox Code Playgroud)

php json file

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

file ×1

json ×1

php ×1