【发布时间】:2016-07-06 13:13:49
【问题描述】:
我很困惑。
我的目标是通过 POST 将数组传递到另一个页面并访问它的数据。我找到了一种使用 serialize() 的方法。
它运行良好,因为我能够在另一页上看到数组,但是当我尝试访问主数组中的嵌套数组时,我没有得到预期的结果。换句话说,我可以访问主数组中的数据,但我得到的是内部数组的“null”。
让我告诉你我做了什么:
//The array:
$cart = &JModelLegacy::getInstance('cart', 'jshop');
$cart->load();
//I can access data of the inner array (the products) within the $cart array, for example:
$productos = $cart->products;
echo "<pre>".json_encode($cart, JSON_PRETTY_PRINT)."</pre>";
echo "<pre>".json_encode($productos[1], JSON_PRETTY_PRINT)."</pre>";
//Then serializing it:
$serializedcart = serialize($cart);
//Then sending it in a form using POST
<input type='hidden' name='cartserialized' value='<?php print $serializedcart?>'>
....然后在另一页上:
// I unserialize the transferred array:
$carretilla = unserialize($_POST[cartserialized]);
// And this doesn't work anymore, I get "null" for $productos:
$productos = $carretilla->products;
echo "<pre>".json_encode($carretilla, JSON_PRETTY_PRINT)."</pre>";
echo "<pre>".json_encode($productos[1], JSON_PRETTY_PRINT)."</pre>";
为什么?任何帮助将不胜感激。
这是序列化前的输出:($cart 和购物车内产品的输出。)
{
"type_cart": "cart",
"products": [
{
"quantity": 1,
"product_id": 329,
"category_id": "17",
"tax": null,
"tax_id": "0",
"product_name": "ATX Cromo Puro",
"thumb_image": "thumb_882-2.jpg",
"delivery_times_id": "0",
"ean": "882-2",
"attributes": "a:1:{i:1;i:28;}",
"attributes_value": [
{
"attr_id": 1,
"value_id": 28,
"attr": "Color",
"value": "Cromo"
}
],
"extra_fields": [],
"weight": "0.0000",
"vendor_id": "1",
"files": "a:0:{}",
"freeattributes": "a:0:{}",
"manufacturer": "Cross",
"pid_check_qty_value": "A:218",
"price": 570,
"href": "\/index.php\/tienda\/product\/view\/17\/329",
"free_attributes_value": []
},
{
"quantity": 3,
"product_id": 469,
"category_id": "21",
"tax": null,
"tax_id": "0",
"product_name": "Bater\u00eda Auxiliar",
"thumb_image": "thumb_JK-PB035.jpg",
"delivery_times_id": "0",
"ean": "JK-PB035",
"attributes": "a:0:{}",
"attributes_value": [],
"extra_fields": [],
"weight": "35.0000",
"vendor_id": "1",
"files": "a:0:{}",
"freeattributes": "a:0:{}",
"manufacturer": null,
"pid_check_qty_value": "P:469",
"price": 265,
"href": "\/index.php\/tienda\/product\/view\/21\/469",
"free_attributes_value": []
}
],
"count_product": 4,
"price_product": 1365,
"summ": 0,
"rabatt_id": 0,
"rabatt_value": 0,
"rabatt_type": 0,
"rabatt_summ": 0,
"model_temp_cart": "tempcart",
"price_product_brutto": 1365
}
{
"quantity": 3,
"product_id": 469,
"category_id": "21",
"tax": null,
"tax_id": "0",
"product_name": "Bater\u00eda Auxiliar",
"thumb_image": "thumb_JK-PB035.jpg",
"delivery_times_id": "0",
"ean": "JK-PB035",
"attributes": "a:0:{}",
"attributes_value": [],
"extra_fields": [],
"weight": "35.0000",
"vendor_id": "1",
"files": "a:0:{}",
"freeattributes": "a:0:{}",
"manufacturer": null,
"pid_check_qty_value": "P:469",
"price": 265,
"href": "\/index.php\/tienda\/product\/view\/21\/469",
"free_attributes_value": []
}
并在序列化和发送之后:
{
"__PHP_Incomplete_Class_Name": "jshopCart",
"type_cart": "cart",
"products": [
{
"quantity": 1,
"product_id": 329,
"category_id": "17",
"tax": null,
"tax_id": "0",
"product_name": "ATX Cromo Puro",
"thumb_image": "thumb_882-2.jpg",
"delivery_times_id": "0",
"ean": "882-2",
"attributes": "a:1:{i:1;i:28;}",
"attributes_value": [
{
"attr_id": 1,
"value_id": 28,
"attr": "Color",
"value": "Cromo"
}
],
"extra_fields": [],
"weight": "0.0000",
"vendor_id": "1",
"files": "a:0:{}",
"freeattributes": "a:0:{}",
"manufacturer": "Cross",
"pid_check_qty_value": "A:218",
"price": 570,
"href": "\/index.php\/tienda\/product\/view\/17\/329",
"free_attributes_value": []
},
{
"quantity": 3,
"product_id": 469,
"category_id": "21",
"tax": null,
"tax_id": "0",
"product_name": "Bater\u00eda Auxiliar",
"thumb_image": "thumb_JK-PB035.jpg",
"delivery_times_id": "0",
"ean": "JK-PB035",
"attributes": "a:0:{}",
"attributes_value": [],
"extra_fields": [],
"weight": "35.0000",
"vendor_id": "1",
"files": "a:0:{}",
"freeattributes": "a:0:{}",
"manufacturer": null,
"pid_check_qty_value": "P:469",
"price": 265,
"href": "\/index.php\/tienda\/product\/view\/21\/469",
"free_attributes_value": []
}
],
"count_product": 4,
"price_product": 1365,
"summ": 0,
"rabatt_id": 0,
"rabatt_value": 0,
"rabatt_type": 0,
"rabatt_summ": 0,
"model_temp_cart": "tempcart",
"price_product_brutto": 1365
}
null
【问题讨论】:
-
在尝试反序列化之前,您是否验证过 $_POST[cartserialized] 在其他页面上有内容?
-
使用 var_dump,检查你是在对象序列化上做了浅拷贝还是深拷贝。
-
是的。它具有您在输出中看到的数据(已编辑问题)。唯一的区别是一个额外的键(值)“__PHP_Incomplete_Class_Name”,我不知道它来自哪里。至于浅拷贝或深拷贝。我不知道你在说什么 Bonatti。谢谢大家。
标签: php arrays post multidimensional-array deserialization