2016-10-03 9 views
0

'$ _SESSION' 배열에서 객체를 제거하는 데 문제가 있습니다. 내 목표는 특정 제품을 선택한 후 배열에서 각 제품을 삭제하는 것입니다. 이 뷰의 일부입니다

public function deleteProductAction() {  
     $productID = $this->_getParam('id', 0); 
     session_start(); 
     $obj = $_SESSION['products'];  
     foreach ($obj as $key => $product) { 
      if ($product['product_id'] == $productID) { 
       unset($product); 
      } 
     } 
     $_SESSION['products'] = $obj;  
    } 

$ OBJ를 printing_r 후 :

<?php     
    for ($i=0; $i < count($this->products); $i++) { 
    echo "<a class='remove_from_basket' href='" .$this->baseUrl. "/shop/delete-product/id/" .$this->products[$i]->product_id. "'>Delete</a>"; 
    }  
?> 

그런 다음 PHP 부분에 내가 함께이 제품 ID를 얻을 수

Array 
(
    [2] => Zend_Db_Table_Row Object 
     (
      [_data:protected] => Array 
       (
        [product_id] => 26 
       ) 

      [_cleanData:protected] => Array 
       (
        [product_id] => 26 

      [_modifiedFields:protected] => Array 
       (
       ) 

      [_table:protected] => 
      [_connected:protected] => 
      [_readOnly:protected] => 
      [_tableClass:protected] => Application_Model_DbTable_Products 
      [_primary:protected] => Array 
       (
        [1] => product_id 
       ) 

     ) 

    [3] => Zend_Db_Table_Row Object 
     (
      [_data:protected] => Array 
       (
        [product_id] => 26 
       ) 

      [_cleanData:protected] => Array 
       (
        [product_id] => 26 
       ) 

      [_modifiedFields:protected] => Array 
       (
       ) 

      [_table:protected] => 
      [_connected:protected] => 
      [_readOnly:protected] => 
      [_tableClass:protected] => Application_Model_DbTable_Products 
      [_primary:protected] => Array 
       (
        [1] => product_id 
       ) 

     ) 

) 

그러나, 아무 일도 일어나지 않습니다 ... 아무도 도와 줄 수 있습니까?

답변

3

코드에서 세션 변수와 완전히 관련이없는 $product을 설정 해제 중입니다. 개체의 index을 사용하고 원래 개체에서 설정을 해제해야합니다. 위의 코드가 완전히 인덱스를 제거

foreach ($obj as $key => $product) { 
    if ($product['product_id'] == $productID) { 
     unset($obj[$key]); 
    } 
} 

: 같은

foreach ($_SESSION['products'] as $key => $product) { 
    if ($product['product_id'] == $productID) { 
     unset($_SESSION['products'][$key]); 
    } 
} 

또는 당신이하고 싶은 $obj에 대한

, 당신은 할 수 있습니다.

+0

이 부분을 표시 할 때보기 부분에 표시 할 때주의 할 점 : 정의되지 않은 오프셋 : 0 – Peter

+0

@Peter 얻을 수있는 방법이 없습니다. 어떤 코드 블록이이를 생성 했습니까? –

+0

첫 번째 for 루프. – Peter