Michael Eugene Yuen
Michael Eugene Yuen

Reputation: 2528

foreach in foreach condition filterings

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;

Upvotes: 2

Views: 410

Answers (3)

RJParikh
RJParikh

Reputation: 4166

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
        )

)

Upvotes: 2

Suyog
Suyog

Reputation: 2482

You can try following trick, using in_array function.

$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';

$promo_brands = array();
$promo_items = array();
$regular_items = array();
$temp_regular_items = array();

foreach ($carts as $cart=>$c) {
    foreach ($offers as $offer=>$o) {
        if ($o['buy_type'] == 'brand' && $o['buy_type_id'] == $c['brand_id']) {
            $promo_brands[$cart] = $c;
        } else if ($o['buy_type'] == 'item' && $o['buy_type_id'] == $c['item_id']) {
            $promo_items[$cart] = $c;
        } else {
            $regular_items[$cart] = $c;
        }
    }
}

foreach($regular_items as $regitem => $r)
{
    if(!in_array($r, $promo_brands) && !in_array($r, $promo_items))
        $temp_regular_items[$regitem] = $r;
}


echo 'Promo Brands<br />';  
echo "<pre>"; print_r($promo_brands); echo "</pre>";
echo '<br />Promo Items<br />';
echo "<pre>"; print_r($promo_items); echo "</pre>";
echo '<br />Regular Items<br />';
echo "<pre>"; print_r($temp_regular_items); echo "</pre>";

This gives output as follows:

Promo Brands

Array
(
    [6] => Array
        (
            [name] => apple
            [brand_id] => 1
            [item_id] => 6
        )

)

Promo Items

Array
(
    [2] => Array
        (
            [name] => orange
            [brand_id] => 3
            [item_id] => 2
        )

)

Regular Items

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

)

Hope this will help you..!

Upvotes: 0

happymacarts
happymacarts

Reputation: 2595

are you wanting to retrieve the banana always or do you need to retrieve it by name? try the in_array function

foreach ($carts as $cart=>$c) { 
if(in_array("banana", $c)){ 
    echo "true"; 
}else{ 
    echo "nope"; 
} 

Upvotes: 0

Related Questions