将数组转换为Json以保存到数据库

时间:2018-07-23 17:21:27

标签: php arrays json

你好,我有一个像这样的数组:

$data = Array(
[0] => {"detail":"l,red","sku":"#123","price":"50","stok":""}
[1] => {"detail":"l,black","sku":"#123","price":"50","stok":""}
[2] => {"detail":"m,red","sku":"#123","price":"50","stok":""}
[3] => {"detail":"m,black","sku":"#123","price":"50","stok":""}
[4] => {"detail":"s,red","sku":"#123","price":"50","stok":""}
[5] => {"detail":"s,black","sku":"#123","price":"50","stok":""}
)

当我使用这个时:

$data = json_encode($data , JSON_FORCE_OBJECT);

但结果类似:

{"0":"{\"detail\":\"l,red\",\"sku\":\"#123\",\"price\":\"50\",\"stok\":\"\"}","1":"{\"detail\":\"l,black\",\"sku\":\"#123\",\"price\":\"50\",\"stok\":\"\"}","2":"{\"detail\":\"m,red\",\"sku\":\"#123\",\"price\":\"50\",\"stok\":\"\"}","3":"{\"detail\":\"m,black\",\"sku\":\"#123\",\"price\":\"50\",\"stok\":\"\"}","4":"{\"detail\":\"s,red\",\"sku\":\"#123\",\"price\":\"50\",\"stok\":\"\"}","5":"{\"detail\":\"s,black\",\"sku\":\"#123\",\"price\":\"50\",\"stok\":\"\"}"}

我不喜欢“ \”结果。有什么问题吗?

1 个答案:

答案 0 :(得分:2)

如果您不喜欢这些斜杠,则应首先解码内部JSON字符串:

setenv