0

Here is my code:

$stack = array();
foreach ($quote->getAllItems() as $item) {
       $PID = $item->getProduct()->getId();
        $stack["productid"][] = $PID;
        $QTY = $item->getQty();
        $stack["productqty"][] = $QTY;

}


foreach($stack as $value){
 $ProductId = $value["productid"];
 $ProductQty = $value["productqty"];
 echo "ProductId is: $ProductId - Product QTY is: $ProductQty <br>";
}

I receive two rows as the exact number of results is but $ProductId and $ProductQty seems to be blank.

Where is my mistake why i can not display the results by key?

Thanks in advance!

5
  • what is the array structure of $stack Commented Jul 15, 2016 at 12:29
  • I have updated my question. Commented Jul 15, 2016 at 12:30
  • Use this code : foreach($stack as $key => $value){ } Commented Jul 15, 2016 at 12:34
  • Can you please make a complete answer so i can test and mark it ? Commented Jul 15, 2016 at 12:35
  • Please post your full code with query Commented Jul 15, 2016 at 12:35

3 Answers 3

2
 if(!empty($stack) && count($stack)>0)
 {
     foreach($stack['productid'] as $key=>$value){
         $ProductId = $value;
         $ProductQty = $stack["productqty"][$key];
         echo "ProductId is: $ProductId - Product QTY is: $ProductQty <br>";
     }
 }

Use this code..

Sign up to request clarification or add additional context in comments.

Comments

0

You just try with below code

foreach($stack as $value){
     $ProductId = $value[0];
    $ProductQty = $value[1];
    echo "ProductId is: $ProductId - Product QTY is: $ProductQty <br>";
 }

The reason is, in the first iteration, the $value will contain the value of $stack["productid"]. So that $value["productid"] won't work. You have to give the index 0. And for the $ProductQty you have to give index 1

2 Comments

Not working. the result is: ProductId is: 30029 - Product QTY is: 30030 ProductId is: 1 - Product QTY is: 3 which is not corect. QTY for the first item is 1, for the second is 3.
@TonyStark, What is result of print_r($stack)??
0

Try this code. it will help full....

foreach ($quote->getAllItems() as $item) {
        $stack[] = array('productid'=>$item->getProduct()->getId(),'productqty'=>$item->getQty());
}

foreach($stack as $value){ 

     $ProductId = $value["productid"];
     $ProductQty = $value["productqty"];
     echo "ProductId is: $ProductId - Product QTY is: $ProductQty <br>";

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.