0

我在将 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);


}
4

1 回答 1

0

根据json_encode()文档,该函数转义正斜杠,尝试这样做:

$value['src'] = str_replace('\\/', '/', $src)
于 2012-05-28T10:46:59.347 回答