Nick
Nick

Reputation: 2980

PHP Logic, testing values of arrays

I've got a slight problem with figuring out the logic for the following problem.

I have a webshop which sells ankle braces. Left and Right. For each brace a scan of the corresponding ankle is neccesary. So, for example, I cannot buy a left brace if I only have a scan of the right ankle. I have two arrays. First array contains the scan data and the second is the web cart containing all the products. But I can't wrap my head around the logic:

return false if a product is in the cart while its scan is not available.

I could just do a bunch of if statements but i'm certain that there is a logical and cleaner way.

Here are the arrays

Array
(
    [left] => Array
        (
            [0] => data
            [1] => data2
            [2] => data3
        )

    [right] => Array
        (
            [0] => data
            [1] => data2
            [2] => data3
        )

)

Array
(
    [product_id1] => Array
        (
            [var] => val
            [side] => left
        )

    [product_id2] => Array
        (
            [var] => val
            [side] => right
        )

)

Upvotes: 0

Views: 73

Answers (1)

Ziarno
Ziarno

Reputation: 7572

Something like this might help:

function check($product) {
    if ($product['side'] == 'left' && !isset($scans['left']) || 
        $product['side'] == 'right' && !isset($scans['right'])) {
          return false;
        }
    return true;
}

foreach ($cart as $product) {
  check($product);
}

Upvotes: 1

Related Questions