我很困惑。
我的目标是通过 POST 将数组传递到另一个页面并访问它的数据。 我找到了一种使用 serialize() 的方法。
它工作正常,因为我能够在另一页上看到数组,但是当我尝试访问主数组中的嵌套数组时,我没有得到预期的结果。换句话说,我可以访问主数组内的数据,但内部数组的"null"为"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": "Bateru00eda 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": "Bateru00eda 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": "Bateru00eda 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
为了在 PHP 中反序列化对象,您需要先加载该类。因此,请包含定义此对象类型的脚本,然后反序列化。
谢谢你们。
我尝试按照其他论坛的建议使用 json_encode 和 json_decode,它似乎效果更好,我在另一端发送和检索数据没有问题。
感谢您的宝贵时间。