Передача сериализованного массива с использованием POST дает неожиданный результат

Я озадачен.

Моя цель — передать массив через POST на другую страницу и получить доступ к его данным. Я нашел способ сделать это с помощью serialize ().

Он работал нормально, так как я могу видеть массив на другой странице, но когда я пытаюсь получить доступ к вложенному массиву в основном массиве, я не получаю ожидаемых результатов. Другими словами, я могу получить доступ к данным внутри основного массива, но для внутреннего массива я получаю «ноль».

Позвольте мне показать вам, что я сделал:

//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 и продуктов внутри 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

0

Решение

Чтобы десериализовать объект в PHP, вам нужно сначала загрузить класс. Поэтому включите сценарий, который определяет тип этого объекта, а затем десериализовать.

0

Другие решения

Спасибо вам, ребята.

Я попытался использовать json_encode и json_decode, как это предлагалось на другом форуме, и, кажется, это работает лучше, и у меня нет проблем с отправкой и получением данных на другом конце.

Спасибо за ваше драгоценное время.

0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector