0

I'd like to check if array exist by two keys: id and type

This code just check by id:

 if (isset($_POST['type'])) {
   $type = $_POST['type'];
 } 
 else {
   $type = '';
 }

 if (array_key_exists($id, $_SESSION['cart'])) {
        $_SESSION['cart'][$id]['quantity'] += $quantity;
    } else {
        $_SESSION['cart'][$id] = $line;
  }

I have tried with this code but it doesn't work:

 if (array_key_exists($id, $_SESSION['cart']) && array_key_exists($type, $_SESSION['cart'])) {
        $_SESSION['cart'][$id]['quantity'] += $quantity;
    } else {
        $_SESSION['cart'][$id] = $line;
    }

$_SESSION['cart'] is an array contains arrays of $line

 $line = array(
        'id' => $id,
        'type' => $type,
        'quantity' => $quantity,
        'price' => $price,
        'picture' => $dish->getPicture()->getWebPath(),
    );

This is the output of $_SESSION['cart']: enter image description here

As you see in th last array with id 55 and type "french bred" , what I'd like to do is to check if th user chose the same product but a with different type so insert new line else if the same product and the same type so just update quantity.

6
  • 1
    And the problem with this code is? Commented Nov 17, 2017 at 15:47
  • What should $_SESSION['cart'] look like? You're currently checking if $_SESSION['cart'][$type] exists. Is that correct? Commented Nov 17, 2017 at 15:48
  • I have updated my question Commented Nov 17, 2017 at 15:53
  • You are searching for a key with value of $id, so if $id is '5' for example, there must be a key '5', but the keys in the array are 'id', 'type' etc. You should loop through cart, iterate over each $line and do if($line['id'] === $id) etc Commented Nov 17, 2017 at 15:54
  • I have updated my quetsion with output of $_session['cart'] an some description :) Commented Nov 17, 2017 at 16:07

2 Answers 2

1

Something like this should do, however the question is too vague and too little code is shown for me to properly understand your problem

$lineExists = false;
    foreach($_SESSION['cart'] as $index => $line){
        if($line['id'] === $id)
        {
            $_SESSION['cart'][$index]['quantity'] += $quantity;
            $lineExists = true;
        }
    }
    if(!$lineExists)
    {
        $_SESSION['cart'][] = $newLine;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this code snippet, which may provide some immediate help. A proper explanation would greatly improve its educational value by showing why this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.]
0

If you want to check if type and id exists then you should do something like this

if (array_key_exists('id', $_SESSION['cart']) && array_key_exists('type', $_SESSION['cart'])) {
  // stuff here..
}

if $id is the value of the key id so 'id' => 5 then you should check like this:

if ($_SESSION['cart']['type'] == $id) {
 // stuff here
}

Hope this somewhat helps you further!

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.