在数组中添加超链接时如何删除“ \”元素?

时间:2018-08-18 11:42:08

标签: php json pdo

代码:

$hyperlink = str_replace("%2F","","http://localhost/android/images/");
if($num>0)
{
    $products_arr=array();
    $products_arr["data"]=array();
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
    {
        extract($row);
        $product_item=array(
            "image_name" => $image_name,
            "image_url" => $hyperlink.$file_s
        );

        array_push($products_arr["data"], $product_item);
    }

    echo json_encode($products_arr);
}

我现在使用php创建了一个json文件,我想在数组函数中添加超链接。我正在使用str_replace("%2F","","http://localhost/android/images/");,但是它不起作用,并且json文件如下所示:

{
  "data": [
    {
      "image_name": "cap-1",
      "image_url": "http:\/\/localhost\/android\/images\/Cap-01.png"
    },
    {
      "image_name": "cap-2",
      "image_url": "http:\/\/localhost\/android\/images\/Cap-02.png"
    },
    {
      "image_name": "cap-3",
      "image_url": "http:\/\/localhost\/android\/images\/Cap-03.png"
    },
    {
      "image_name": "ear-1",
      "image_url": "http:\/\/localhost\/android\/images\/Ears-01.png"
    },
    {
      "image_name": "ear-2",
      "image_url": "http:\/\/localhost\/android\/images\/Ears-02.png"
    },
    {
      "image_name": "ear-3",
      "image_url": "http:\/\/localhost\/android\/images\/Ears-03.png"
    },
    {
      "image_name": "hair-1",
      "image_url": "http:\/\/localhost\/android\/images\/Hair Color-01.png"
    },
    {
      "image_name": "hair-2",
      "image_url": "http:\/\/localhost\/android\/images\/Hair Color-02.png"
    }
  ]
}

那么如何从网址中删除“ \”元素?请帮助我。

谢谢

1 个答案:

答案 0 :(得分:0)

JSON_UNESCAPED_SLASHES中调用选项json_encode将解决您的问题。

$example = json_encode($array, JSON_UNESCAPED_UNICODE | 
JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);