PHP json_encode()和javascript

时间:2015-07-24 12:04:28

标签: javascript php arrays json debugging

我的情况:

我在db中有表从中提取数据,然后我使用

进行转换

json_encode()

这是实际代码:

$result = $this->model_catalog_product->getProduct($id);

$images = ($this->model_catalog_product->getProductImages($id));

foreach ($images as $key => $value) 
// Here i am looping the data so i can filter useless data i need only the photo //location which is the actual 
{
  $result['photos'.$key] = $value['image'];
}

var_dump($result)

echo json_encode($result)

这是输出......一切似乎都没问题但是......

array (size=41)
  'product_id' => string '51' (length=2)
  'model' => string 'SAK' (length=3)
  'sku' => string '' (length=0)
  'upc' => string '' (length=0)
  'ean' => string '' (length=0)
  'jan' => string '' (length=0)
  'isbn' => string '' (length=0)
  'mpn' => string '' (length=0)
  'location' => string '' (length=0)
  'quantity' => string '1' (length=1)
  'stock_status_id' => string '6' (length=1)
  'image' => string 'catalog/fk_gewindefahrwerk_slx_2xf_2xg_fkxkkvw68_02.jpg' (length=55)
  'manufacturer_id' => string '0' (length=1)
  'shipping' => string '1' (length=1)
  'price' => string '400.0000' (length=8)
  'points' => string '0' (length=1)
  'tax_class_id' => string '0' (length=1)
  'date_available' => string '2015-07-08' (length=10)
  'weight' => string '0.00000000' (length=10)
  'weight_class_id' => string '1' (length=1)
  'length' => string '0.00000000' (length=10)
  'width' => string '0.00000000' (length=10)
  'height' => string '0.00000000' (length=10)
  'length_class_id' => string '1' (length=1)
  'subtract' => string '1' (length=1)
  'minimum' => string '1' (length=1)
  'sort_order' => string '1' (length=1)
  'status' => string '1' (length=1)
  'viewed' => string '6' (length=1)
  'date_added' => string '2015-07-08 16:43:29' (length=19)
  'date_modified' => string '2015-07-22 21:32:57' (length=19)
  'language_id' => string '1' (length=1)
  'name' => string 'Спортно Окачване "KONI"' (length=48)
  'description' => string '<p><br></p>' (length=29)
  'tag' => string '' (length=0)
  'meta_title' => string 'Спортно окачване KONI BMW' (length=40)
  'meta_description' => string '' (length=0)
  'meta_keyword' => string '' (length=0)
  'keyword' => null
  'photos0' => string 'catalog/fk_gewindefahrwerk_slx_2xf_2xg_fkxkkvw68_02.jpg' (length=55)
  'photos1' => string 'catalog/_goods.jpg' (length=18)
{ "product_id": "51", "model": "SAK", "sku": "", "upc": "", "ean": "", "jan": "", "isbn": "", "mpn": "", "location": "", "quantity": "1", "stock_status_id": "6", "image": "catalog/fk_gewindefahrwerk_slx_2xf_2xg_fkxkkvw68_02.jpg", "manufacturer_id": "0", "shipping": "1", "price": "400.0000", "points": "0", "tax_class_id": "0", "date_available": "2015-07-08", "weight": "0.00000000", "weight_class_id": "1", "length": "0.00000000", "width": "0.00000000", "height": "0.00000000", "length_class_id": "1", "subtract": "1", "minimum": "1", "sort_order": "1", "status": "1", "viewed": "6", "date_added": "2015-07-08 16:43:29", "date_modified": "2015-07-22 21:32:57", "language_id": "1", "name": "Спортно Окачване "KONI"", "description": "<p><br></p>", "tag": "", "meta_title": "Спортно окачване KONI BMW", "meta_description": "", "meta_keyword": "", "keyword": null, "photos0": "catalog/fk_gewindefahrwerk_slx_2xf_2xg_fkxkkvw68_02.jpg", "photos1": "catalog/_goods.jpg" }

所以这里的所有内容似乎都可以正常编码,但这是我在chrome javascript控制台中的var_dump:

date_added: "2015-07-08 16:30:49"
date_available: "2015-07-08"
date_modified: "2015-07-08 16:37:14"
description: "&lt;p&gt;ОПИСАНИЕ&lt;/p&gt;"
ean: ""
height: "0.00000000"
image: ""
isbn: ""
jan: ""
keyword: null
language_id: "1"
length: "0.00000000"
length_class_id: "1"
location: ""
manufacturer_id: "0"
meta_description: ""
meta_keyword: ""
meta_title: "Макферсън BRAND"
minimum: "1"
model: "AKS130"
mpn: ""
name: "Макферсън"
points: "0"
price: "100.0000"
product_id: "50"
quantity: "1"
shipping: "1"
sku: ""
sort_order: "1"
status: "1"
stock_status_id: "6"
subtract: "1"
tag: ""
tax_class_id: "0"
upc: ""
viewed: "3"
weight: "0.00000000"
weight_class_id: "1"
width: "0.00000000"
__proto__: Object

每次都会丢失照片。我尝试作为对象,但结果是一样的。我发展了3年,但我以前从未见过这样的问题。如果你有任何关于这个的线索,请告诉我,我将非常感激。提前致谢

divanche_pras

1 个答案:

答案 0 :(得分:-1)

默认情况下,PHP json_encode转义斜杠

尝试通过执行json_encode($result, JSON_UNESCAPED_SLASHES)

来覆盖这一点