MrFoh
MrFoh

Reputation: 2701

Sorting through array for to find match

I have two arrays like this

Array
(
    [0] => MYU_MP1
    [1] => 4cc00e5f580f00c2e54193fde7129608
    [2] => da8bbfdb40be0dc2b3312ca1037f994a
    [3] => d4cfaa8db24c4b81db506189360b6b99
)
Array
(
    [0] => MYU_SC1
    [1] => MYU_SC2
    [2] => MYU_SF1
    [3] => MYU_SC3
    [4] => MYU_AD1
    [5] => MYU_AD2
    [6] => MYU_AD3
    [7] => MYU_AD4
    [8] => MYU_RC1
    [9] => MYU_RC2
    [10] => MYU_RC3
    [11] => MYU_RC4
    [12] => MYU_RC5
    [13] => MYU_RC6
    [14] => MYU_RC7
    [15] => MYU_RC8
)

the first one aliased as "deliverable" and the other "non_deliverable" and a third array

Array
(
    [d8df18561040f3d9bd9868f5c5aaa7c2] => Array
        (
            [rowid] => d8df18561040f3d9bd9868f5c5aaa7c2
            [id] => MYU_SC1
            [qty] => 1
            [price] => 500
            [name] => WAEC Scratch Card
            [service_image] => assets/img/waec.jpg
            [service_category] => scratch_cards
            [subtotal] => 500
        )

the third array is aliased "cart_items"

    [f68964856a61092d9a2566d024a0ba28] => Array
        (
            [rowid] => f68964856a61092d9a2566d024a0ba28
            [id] => MYU_MP1
            [qty] => 1
            [price] => 210000
            [name] => Apple iPhone 5 16gb
            [service_image] => 
            [service_category] => mobile-phones
            [subtotal] => 210000
        )

)

I have written a function that supposed to loop through the third array to determine if an "id" element of the third array is member of the first or second array

//sort through cart items
    foreach ($cart_items as $key => $item) {
        if(in_array($item['id'], $deliverables) && in_array($item['id'], $non_deliverables)) {
            $deliverable = TRUE;
            $non_deliverable = TRUE;
        }

        if(in_array($item['id'], $deliverables) && !in_array($item['id'], $non_deliverables)) {
            $deliverable = TRUE;
        }

        if(in_array($item['id'], $non_deliverables) && !in_array($item['id'], $deliverables)) {
            $non_deliverable = TRUE;
        }

        if($deliverable && $non_deliverable)
            $type = "both";
        if($non_deliverable)
            $type = "non-deliverable";
        if($deliverable)
            $type = "deliverable";
    }
    return $type;

But when the third array has matches in both the 1st and 2nd array the function returns "deliverable". What am i not doing right?

Upvotes: 0

Views: 64

Answers (2)

Dmytro Zarezenko
Dmytro Zarezenko

Reputation: 10686

function somefunction()
        foreach ($cart_items as $key => $item) {
            $deliverable = in_array($item['id'], $deliverables);
            $non_deliverable = in_array($item['id'], $non_deliverables);

            $type = "none";
            if ($deliverable && $non_deliverable) {
                $type = "both";
            } elseif ($deliverable && !$non_deliverable) {
                $type = "deliverable";
            } elseif (!$deliverable && $non_deliverable) {
                $type = "non-deliverable";
            }
        }
        return $type;
}

Upvotes: 2

Ben
Ben

Reputation: 11188

Your sortorder is wrong. The check for $deliverable and $non_deliverable should be the last. Alternatively you could use an if-else-if-else structure.

Upvotes: 1

Related Questions