我的目标是通过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>";
为什么?任何帮助将不胜感激。
这是串行化之前的输出:($车和产品内购物车的输出。)
{ "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]在另一页上是否有内容? – tjfo
使用var_dump,检查是否只是在对象序列化中做了浅拷贝或深层拷贝。 – Bonatti
是的。它有数据,你可以在输出中看到(问题编辑)。唯一的区别是我不知道它来自哪里的附加关键字(值)“__PHP_Incomplete_Class_Name”。至于浅拷贝或深拷贝,我不知道你在说什么Bonatti。多谢你们。 – ILemus