0
Array
(
    [0] => Array
        (
            [cart_id] => 24763
            [product_id] => 740
            [name] => Samsung S20
            [model] => Samsung S20
            [shipping] => 1
            [image] => catalog/samsung/samsungs20.jpg
            [option] => Array
                (
                )

            [download] => Array
                (
                )
        )

    [1] => Array
        (
            [cart_id] => 24799
            [product_id] => 749
            [name] => Huawei P40
            [model] => Huawei P40
            [shipping] => 1
            [image] => catalog/huawei/huaweip40.jpg
            [option] => Array
                (
                )

            [download] => Array
                (
                )
        )

)

^ Above is the output of the array

$productdata = $this->cart->getProducts(); 

^ Above is the code for query out array

is there anyway to check product_id of each array ? Let say that if product_id = 749 , alert ('P40');

Tried to do

foreach ($productdata as $productdatas) {
    if ($productdata['product_id'] = 749)
    { alert('P40'); 
    }

}

nothing happen

6
  • 1
    Where is your foreach? Commented Jul 29, 2020 at 1:13
  • The code you say you tried has syntax errors so it won't run at all. Please read this to see how to make a minimal, reproducible example so we can see the problem and be able to help. Commented Jul 29, 2020 at 1:19
  • @catcon added in first post Commented Jul 29, 2020 at 1:20
  • 1
    Take a look at the variables you use in foreach and then look at which of these variables you use to check the product id... do you see the problem now? Commented Jul 29, 2020 at 1:22
  • 2
    @FluffyKitten my bad , its working now , look like a mistake that shouldn't happen. thanks Commented Jul 29, 2020 at 1:40

1 Answer 1

1

This part sets the key, not read it. You need to replace the = with ==:

// From
if ($productdata['product_id'] = 749)

// To
if ($productdata['product_id'] == 749)
Sign up to request clarification or add additional context in comments.

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.