代码:
$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"
}
]
}
那么如何从网址中删除“ \”元素?请帮助我。
谢谢
答案 0 :(得分:0)
在JSON_UNESCAPED_SLASHES
中调用选项json_encode
将解决您的问题。
$example = json_encode($array, JSON_UNESCAPED_UNICODE |
JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);