我的目標是通過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