2016-06-13 3 views
4

내 문제. 나는 연관 배열을 가지며 키가 함수에 전달 된 항목 객체의 항목 ID와 일치하기를 원합니다. 항목 ID 키가 배열에 아직없는 경우 키 값으로 ["Item"=>$item,"Quantity=>1]이라는 새 배열과 함께 항목 ID를 키로 배열 카트에 추가하고 싶습니다.Php 연관 배열 값 삽입, 잘못된 키 표시

키가 이미 존재하는 경우 카트 어레이에 항목 ID를 인덱싱하여 검색 할 배열에 저장된 수량을 업데이트하고 싶습니다. 다음

내가 (Cart.class.php에 위치) 그 결과를 것이라고 생각하는 것이이 코드입니다 : 다음 var_dump($this->cart_items)를 사용하는 경우,

private $cart_items = array(); 

    public function add_to_cart($item, $quantity = 1){ 
    if(!isset($item) || !isset($item->ItemID)){ 
     throw new Exception("Error adding item to cart"); 
    } 
    if(!array_key_exists($item->ItemID,$this->cart_items)){ 
     $this->cart_items[$item->ItemID] = array("Item"=>$item,"Quantity"=>$quantity); 
    }else{ 
     $this->cart_items[$item->ItemID]["Quantity"] += $quantity; 
    } 
    $this->number_of_cart_items+=$quantity; 
} 

그러나되어 출력 :

array(2){ 
    [ 
     0 
    ] => NULL [ 
     1 
    ] => array(2) { 
     [ 
     "Item" 
     ]  => object(stdClass)#2 (8)  { 
     [ 
      "ItemID" 
     ]   => int(11)   [ 
      "ItemName" 
     ]   => string(18) "Kids check T-Shirt"   [ 
      "ShortDescription" 
     ]   => string(20) "A kids check T-Shirt"   [ 
      "LongDescription" 
     ]   => string(51) " A kids check T-shirt perfect for formal occasions!"   [ 
      "ItemPrice" 
     ]   => float(33.59)   [ 
      "ImagePath" 
     ]   => string(51) "kozzi-26129586-1591x2387.jpg"   [ 
      "QuantityAvailable" 
     ]   => int(100)   [ 
      "ItemSupplier_SupplierID" 
     ]   => int(1) 
     }  [ 
     "Quantity" 
     ]  => int(1) 
    } 
} 

내 문제는 $item->ItemID이 연관 배열의 키로 사용되지 않는다는 것입니다. 키가 [0] [1]이고, 첫 번째 키가 null 인 것을 볼 수 있습니다. $this->cart_items[$item->ItemID] = array()을 사용하고 있는데 제 질문은 : 나는 어떡하지? 잘못하고 배열의 키로 ID가 사용되지 않는 이유는 무엇입니까?

감사합니다.

+0

가 add_to_cart 기능 – Terminus

+0

예 내부로부터 위해서 var_dump ($ 항목) 수행을 참조하십시오,하지만 난 항목 ID는 또한 cart_items 연관 배열에 대한 인덱스가 될 것을 싶습니다. – Eladian

+1

$ item에 var_dump를 입력하고 결과를 여기에 게시하십시오. – Terminus

답변

-2

$ item-> ItemID을 (를) 해 보았습니까? 실수가 아니라면 연관 배열 키는 문자열이어야합니다. 그렇지 않으면 키가 아닌 배열 인덱스를 사용하고 있습니다.

+0

당신은 실수했다. – Terminus

0

코드가 제대로 작동합니다.

class Cart{ 

    private $cart_items = array(); 
    private $number_of_cart_items; 

    public function add_to_cart($item, $quantity = 1){ 
     if(!isset($item) || !isset($item->ItemID)){ 
      throw new Exception("Error adding item to cart"); 
     } 
     if(!array_key_exists($item->ItemID,$this->cart_items)){ 
      $this->cart_items[$item->ItemID] = array("Item"=>$item,"Quantity"=>$quantity); 
     }else{ 
      $this->cart_items[$item->ItemID]["Quantity"] += $quantity; 
     } 
     $this->number_of_cart_items+=$quantity; 
     return $this->cart_items; 
    } 
} 


$itemArray = array(
    "ItemID" => 11, 
    "ItemName" => "Kids check T-Shirt", 
    "ShortDescription" => "A kids check T-Shirt", 
    "LongDescription"=>"A kids check T-shirt perfect for formal occasions!", 
    "ItemPrice" => 33.59, 
    "ImagePath" => "kozzi-26129586-1591x2387.jpg", 
    "QuantityAvailable" => 100, 
    "ItemSupplier_SupplierID" => 1 
); 
$quantity = 1; 
$item = (object)$itemArray ; 


$cart = new Cart(); 
$add_to_cart = $cart->add_to_cart($item,$quantity); 


print_r($add_to_cart);  
var_dump($add_to_cart); 

https://3v4l.org/N96Vc

+0

'add_to_cart()'함수에 전달 된 매개 변수는 ** 배열이 아니다 **; OP가 말했듯이, 그것은 "item object passed"*입니다. 설명을 캐리하게 읽으십시오! –

+1

배열을 함수로 전달하기 전에 객체로 변환 중입니다 .kindly $ item = (object) $ itemArray; 라인을 확인하십시오. @GergelyLukacsy – zakhefron

+0

아, 알겠습니다 ... +1! –