如何使用continue继续在foreach循环中获取正确的值;

时间:2015-09-25 09:32:17

标签: php

我在试图获得正确的价值方面遇到了一些麻烦,但这让我有些痛苦。

在我的代码下面做了一个循环通过项目并查找该值是否满足要求并且var_dump结果但是不正确,我也在下面写出结果我需要的输出。

    $requeriment = 250;
    $items = array(
        array(
            'id' => 12,
            'price' => 199, // 2%
            'quantity' => 1
        ),
        array(
            'id' => 13,
            'price' => 199, // 2%
            'quantity' => 1
        ),
        array(
            'id' => 14,
            'price' => 199, // 2%
            'quantity' => 1
        ),
        array(
            'id' => 15,
            'price' => 399, // 1%
            'quantity' => 1
        )
    );

    $flag = false;
    $sumItems = 0;

    foreach($items as $item){
        $totalItem = $item['price'] * $item['quantity'];

        if($totalItem >= $requeriment){
            if(!$flag){
                $flag = true;
                $itemIds[] = array(
                    '1% Disc' => $item['id']
                );
            }else if($flag){
                $itemIds[] = array(
                    '2% Disc' => $item['id']
                );
            }
            continue;
        }

        //If no product with value required lets sum until we get that amount
        $sumItems += $item['price'] * $item['quantity'];
        if($sumItems >= $requeriment){
            $itemIds[] = array(
                '1% Disc' => $item['id']
            );
            continue;
        }
        $itemIds[] = array(
            '2% Disc' => $item['id']
        );



    }//THE END

    var_dump($itemIds);

    //the result is: 
    array(4) {
      [0]=>
      array(1) {
        ["2% Disc"]=>
        int(12)
      }
      [1]=>
      array(1) {
        ["1% Disc"]=>
        int(13)
      }
      [2]=>
      array(1) {
        ["1% Disc"]=>
        int(14)
      }
      [3]=>
      array(1) {
        ["1% Disc"]=>
        int(15)
      }
    }

    // but what i need is:

    array(4) {
      [0]=>
      array(1) {
        ["2% Disc"]=>
        int(12)
      }
      [1]=>
      array(1) {
        ["2% Disc"]=>
        int(13)
      }
      [2]=>
      array(1) {
        ["2% Disc"]=>
        int(14)
      }
      [3]=>
      array(1) {
        ["1% Disc"]=>
        int(15)
      }
    }

1 个答案:

答案 0 :(得分:1)

我想我已经开始工作了,不知道第二个循环是否需要标志,但是它返回了我需要的东西:

正如@MetalMichael所说,也许我需要第二个循环,他是对的!

$requeriment = 250;
    $items = array(
        array(
            'id' => 12,
            'price' => 199, // 2%
            'quantity' => 1
        ),
        array(
            'id' => 13,
            'price' => 199, // 2%
            'quantity' => 1
        ),
        array(
            'id' => 14,
            'price' => 199, // 2%
            'quantity' => 1
        ),
        array(
            'id' => 15,
            'price' => 199, // 1%
            'quantity' => 1
        )
    );

    $flag = false;
    $sumItems = 0;
    $itemIds = array();

    //Get if value is bigger than required.
    foreach($items as $item){
        $totalItem = $item['price'] * $item['quantity'];
        if($totalItem >= $requeriment){
            if(!$flag){
                $flag = true;
                $itemIds[] = array(
                    '1% Disc' => $item['id']
                );
            }
        }
        continue;
    }

    //If first loop get no result lets sum items and get the ids to apply the first discount
    if(!count($itemIds) > 0){
        foreach($items as $item){
            $sumItems += $item['price'] * $item['quantity'];
            $itemIds[] = array(
                '1% Disc' => $item['id']
            );
            if($sumItems >= $requeriment){
                if(!$flag){
                    $flag = true;
                    break;
                }
            }
        }
    }

    var_dump($itemIds);