Michael Eugene Yuen Michael Eugene Yuen - 6 months ago 10
PHP Question

foreach in foreach filtering

Sorry I don't know how to provide a more appropriate subject

How can I get only the remaining arrays (in this case: banana) in the $regular_items array (which doesn't fall into the first 2 conditions)?
I have tried various if conditions for days still doesn't work.

The following codes give me all 3 $cart array in the $regular_items array.

<?php
$offers[0]['buy_type'] = 'brand';
$offers[0]['buy_type_id'] = '1';
$offers[0]['name'] = 'brand1';

$offers[1]['buy_type'] = 'item';
$offers[1]['buy_type_id'] = '2';
$offers[1]['name'] = 'item2';

$carts[2]['name'] = 'orange';
$carts[2]['brand_id'] = '3';
$carts[2]['item_id'] = '2';

$carts[4]['name'] = 'banana';
$carts[4]['brand_id'] = '6';
$carts[4]['item_id'] = '4';

$carts[6]['name'] = 'apple';
$carts[6]['brand_id'] = '1';
$carts[6]['item_id'] = '6';

foreach ($carts as $cart=>$c) {
foreach ($offers as $offer=>$o) {

if ($o['buy_type'] == 'brand' && $o['buy_type_id'] == $c['brand_id']) {
// if this condition is true, add to $promo_brands array
$promo_brands[$o['buy_type_id']][$cart] = $c;
} else if ($o['buy_type'] == 'item' && $o['buy_type_id'] == $c['item_id']) {
// otherwise if this condition is true, add to $promo_items array
$promo_items[$cart] = $c;
} else {
// if none of the above, add to $regular_items
$regular_items[$cart] = $c;
}
}
}
echo 'Promo Brands<br />';
var_dump($promo_brands);
echo '<br />Promo Items<br />';
var_dump($promo_items);
echo '<br />Regular Items<br />';
var_dump($regular_items);
?>


Desired OUTPUT

Promo Brands
array(1) { [1]=> array(1) { [6]=> array(3) { ["name"]=> string(5) "apple" ["brand_id"]=> string(1) "1" ["item_id"]=> string(1) "6" } } }
Promo Items
array(1) { [2]=> array(3) { ["name"]=> string(6) "orange" ["brand_id"]=> string(1) "3" ["item_id"]=> string(1) "2" } }
Regular Items
array(1) { [4]=> array(3) { ["name"]=> string(6) "banana" ["brand_id"]=> string(1) "6" ["item_id"]=> string(1) "4" } }


SOLVED:

unsetting the array elements once it meets the condition before going to another condition(s)

$tmp_carts = $carts;
foreach ($tmp_carts as $cart=>$c) {
foreach ($offers as $offer=>$o) {

if ($o['buy_type'] == 'brand' && $o['buy_type_id'] == $c['brand_id']) {
// if this condition is true, add to $promo_brands array
$promo_brands[$o['buy_type_id']][$cart] = $c;
unset($tmp_carts[$cart]);
} else if ($o['buy_type'] == 'item' && $o['buy_type_id'] == $c['item_id']) {
// otherwise if this condition is true, add to $promo_items array
$promo_items[$cart] = $c;
unset($tmp_carts[$cart]);
} else {

}
}
}

$regular_items = $tmp_carts;

Answer

Just you need to check

Use if condition.

if($c['name'] == "banana")
{
    $regular_items[$cart] = $c;
}

OR

Use in_array().

if(in_array("banana", $c))
{
    $regular_items[$cart] = $c;
}

Code

$offers[1]['buy_type'] = 'item';
$offers[1]['buy_type_id'] = '2';
$offers[1]['name'] = 'item2';

$carts[2]['name'] = 'orange';
$carts[2]['brand_id'] = '3';
$carts[2]['item_id'] = '2';

$carts[4]['name'] = 'banana';
$carts[4]['brand_id'] = '6';
$carts[4]['item_id'] = '4';

$carts[6]['name'] = 'apple';
$carts[6]['brand_id'] = '1';
$carts[6]['item_id'] = '6';

foreach ($carts as $cart=>$c) {
    foreach ($offers as $offer=>$o) {
        if ($o['buy_type'] == 'brand' && $o['buy_type_id'] == $c['brand_id']) {
            $promo_brands[$o['buy_type_id']][$cart] = $c;
        } else if ($o['buy_type'] == 'item' && $o['buy_type_id'] == $c['item_id']) {
            $promo_items[$cart] = $c;
        } else {
            if($c['name'] == "banana")
            {
                $regular_items[$cart] = $c;
            }
        }
    }
}
echo '<br />Regular Items<br />';   
print_r($regular_items);
?>

Output

Regular Items
Array
(
    [4] => Array
        (
            [name] => banana
            [brand_id] => 6
            [item_id] => 4
        )

)

Use unset() to remove array elements which already comes in condition.

foreach ($carts as $cart=>$c) {
    foreach ($offers as $offer=>$o) {
        if ($o['buy_type'] == 'brand' && $o['buy_type_id'] == $c['brand_id']) {

            $promo_brands[$o['buy_type_id']][$cart] = $c;
            unset($carts[$cart]);
        } else if ($o['buy_type'] == 'item' && $o['buy_type_id'] == $c['item_id']) {
            $promo_items[$cart] = $c;
            unset($carts[$cart]);
        }
    }
}

echo "<pre>";
print_r($carts);

Output

Array
(
    [4] => Array
        (
            [name] => banana
            [brand_id] => 6
            [item_id] => 4
        )

)
Comments