在foreach条件过滤中的foreach

时间:2016-05-12 06:38:21

标签: php arrays foreach

抱歉,我不知道如何提供更合适的主题

如何在$ regular_items数组中只获取剩余的数组(在本例中为:banana)(不属于前两个条件)? 如果几天的条件仍不起作用,我已尝试了各种方法。

以下代码为$ regular_items数组提供了所有3 $ cart数组。

<?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);
?>

所需的输出

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" } }

解决:

在进入另一个条件之前,一旦满足条件就取消设置数组元素

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

3 个答案:

答案 0 :(得分:2)

您需要检查

使用if条件。

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

使用in_array()

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

<强>代码     

$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);
?>

输出

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

)

使用unset()删除已经处于状态的数组元素。

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);

<强>输出

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

)

答案 1 :(得分:0)

你想要总是检索香蕉还是你需要通过名字检索它?尝试使用in_array函数

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

答案 2 :(得分:0)

您可以使用in_array功能尝试以下技巧。

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

这给出了如下输出:

促销品牌

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

)

宣传品

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

)

常规物品

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

)

希望这会对你有帮助..!