Ich bin verwirrt.Das Übergeben eines serialisierten Arrays mit POST führt zu einem unerwarteten Ergebnis
Mein Ziel ist es, das Array über POST auf eine andere Seite zu übergeben und auf seine Daten zuzugreifen. Ich habe einen Weg gefunden, dies mit serialize() zu tun.
Es funktionierte gut, da ich in der Lage bin, das Array auf der anderen Seite zu sehen, aber wenn ich versuche, auf ein verschachteltes Array innerhalb des Haupt-Array zuzugreifen, bekomme ich nicht die erwarteten Ergebnisse. Mit anderen Worten, ich kann auf die Daten innerhalb des Haupt-Arrays zugreifen, aber ich bekomme "Null" für das innere Array.
Lassen Sie mich Ihnen zeigen, was ich tat:
//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?>'>
.... dann auf der anderen Seite:
// 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>";
Warum? Jede Hilfe wird sehr geschätzt.
Dies ist die Ausgabe vor der Serialisierung: (Output von $ Warenkorb und Produkten innerhalb Warenkorb gelegt.)
{ "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": [] }
Und nach der Serialisierung und Senden:
{ "__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
Haben Sie validiert, dass $ _POST [cartserialized] Inhalt auf der anderen Seite hat, bevor Sie versuchen, es zu deserialisieren? – tjfo
Verwenden Sie die var_dump, überprüfen Sie, ob Sie gerade eine flache Kopie oder eine tiefe Kopie auf Ihre Objektserialisierung vorgenommen haben. – Bonatti
Ja. Es hat Daten, wie Sie auf der Ausgabe sehen können (Frage bearbeitet). Und der einzige Unterschied ist ein zusätzlicher Schlüssel (Wert) "__PHP_Incomplete_Class_Name", von dem ich nicht weiß, woher er kam. Und für seichtes Kopieren oder tiefes Kopieren .. Ich habe keine Ahnung, wovon du redest, Bonatti. Danke Leute. – ILemus