2016-07-06 2 views
0

나는 혼란 스럽습니다.POST를 사용하여 직렬화 된 배열을 전달하면 예상치 못한 결과가 발생합니다.

제 목표는 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

+0

$ _POST [cartserialized]에 직렬화를 시도하기 전에 다른 페이지의 내용이 있는지 확인 했습니까? – tjfo

+0

var_dump를 사용하여 얕은 복사본을 만들었는지 확인하거나 개체 직렬화에 대한 전체 복사본을 확인하십시오. – Bonatti

+0

예. 그것은 당신이 산출물 (편집 된 질문)에서 볼 수있는 데이터를 가지고 있습니다. 유일한 차이점은 어디에서 왔는지 알지 못하는 추가 키 (값) "__PHP_Incomplete_Class_Name"입니다. 그리고 얕은 사본 또는 전체 복사본에 대해서는 Bonatti에 대해 무엇을 말하는지 전혀 알지 못합니다. 고마워. – ILemus

답변

0

역 직렬화하기 위해서 PHP에서 객체를 사용하려면 클래스를 먼저로드해야합니다. 따라서이 객체의 유형을 정의하는 스크립트를 포함시킨 다음 역 직렬화하십시오.

+0

자세한 내용은 [객체 직렬화] (http://php.net/manual/en/language.oop5.serialization.php)를 참조하십시오. – showdev

0

감사합니다.

다른 포럼에서 제안한대로 json_encode 및 json_decode를 사용하여 시도해 보았는데 더 잘 작동하는 것처럼 보였습니다. 다른 쪽 끝에서 데이터를 보내고 검색하는 데 문제가 없습니다.

감사합니다.

관련 문제