Php 在文件操作后重新编码json

Php 在文件操作后重新编码json,php,json,file-io,Php,Json,File Io,我在将json重新编码回其格式时遇到了一些问题。我试图实现的是,在.json文件上传时,修改它以满足某些标准 问题是,当我试图修改文件上的图像src并将其编码回json时,尽管foreach循环中的数组打印正确,但生成的新文件没有任何内容 很高兴你能在这件事上给我点帮助 function prepareImages($file,$url,$article) { $string = file_get_contents($file); $json = json_decode($st

我在将json重新编码回其格式时遇到了一些问题。我试图实现的是,在.json文件上传时,修改它以满足某些标准

问题是,当我试图修改文件上的图像src并将其编码回json时,尽管foreach循环中的数组打印正确,但生成的新文件没有任何内容

很高兴你能在这件事上给我点帮助

function prepareImages($file,$url,$article)
{

    $string = file_get_contents($file);
    $json = json_decode($string,true);
    $value = array();
    //$array = $json['elements'];
            //$imgArray = array();
    /* if(['type'] == "image")
    {
        array_push($json['elements']['src'],$array);
    } */

    foreach($json['elements'] as $value)
    {
        if($value['type'] == "image")
        {
                        $arr = explode('\\', $value['src']);
                        $count = count($arr);
                        $img = $arr[$count-1];
                        $src = $url.'res/'.$article.'/'.$img;
                        $value['src'] = $src;

                        //print_r($json);
        }
                    //This prints all the values correctly
                    print_r($value);


    } 
                    $json = $value;
                    $string = json_encode($json);

            file_put_contents($file,$string);


}
根据文档,该函数将跳过正向斜杠,请尝试执行以下操作:

$value['src'] = str_replace('\\/', '/', $src)

问题不在于字符转义,而是生成的.json文件为空!回显$string返回什么?如果失败,文件内容将返回false,或者成功时更改的bute数。它返回什么?这是我从文件中得到的内容“文件上载成功158”其他脚本是否会覆盖此文件?file_put_contents不检查字符串是否为空,请尝试添加if(!empty($string)){file_put_contents($file,$string);}并查看是否仍存在这种情况。