嵌套的foreach范围中的布尔值会产生意外值

时间:2017-05-15 10:49:54

标签: php arrays scope

我有一个数组数组。他们都是订阅研讨会的人。在表的末尾添加了一些追加销售的产品。问题是:有时候有人订购了2个人,并且属于交易的追加销售产品,而不是一个人加入了这两个人。因此,如果我注册2个人并购买2件衬衫,那么该名单将两件衬衫两次。

所以我想:让我们循环一下,看看我是否找到一些加售,然后用一个简单的双foreach删除每一次出现。但$first的值始终为true。如果我在声明下方回应它,那么它是false。我多次回应这些值,并在代码的注释中添加了值。

foreach($results as $key => $result){
    if(!empty($result["upsell"])){
        $tid = $result["id"];
        $first = true;
        foreach($results as $result2){
            if($result2["id"] == $tid){
                // ALWAYS TRUE
                if(!$first){
                    $results[$key]["upsell"] = "";
                }
                $first = false;
                // FALSE
            }
        }
    }
}

这里有一些奇怪的范围吗?在我看来,它应该首先循环然后才是真实的,然后循环第二个并且是假的。

编辑,添加了print_r个结果。

Array
(
   [28] => Array
    (
        [id] => 1475
        [transaction_id] => SAME
        [club] => xxxxxxxx
        [event] => Voetbalmiddag 
        [category] => Scoutingdagen
        [date_from] => 2017-05-31
        [date_end] => 2017-05-31
        [first_name] => xxxxxx
        [last_name] => xxxxxx
        [birth_date] => xxxxxxx
        [email] => xxxx@hotmail.com
        [address] => xxxxx
        [zipcode] => 8xxxxx
        [city] => xxxxxx
        [phone] => xxxxxx
        [soccer_club] => xxxxxxx
        [soccer_team] => MP JO7-2
        [position] => field
        [printed] => 0
        [print_name] =>  
        [print_number] =>  
        [shirt_size] => 
        [sock_size] => 
        [pants_size] => 
        [referral] => Via de sportvereniging
        [comments] =>  
        [status] => paid
        [upsell] => 2 Shirt
    )

[29] => Array
    (
        [id] => 1476
        [transaction_id] => SAME
        [club] => xxxxxxx
        [event] => Voetbalmiddag
        [category] => Scoutingdagen
        [date_from] => 2017-05-31
        [date_end] => 2017-05-31
        [first_name] => xxxx
        [last_name] => xxxxxxx
        [birth_date] => xxxxxxx
        [email] => xxxxx@hotmail.com
        [address] => Ixxxxx
        [zipcode] => xxxxx
        [city] => xxxxxx
        [phone] => xxxxx
        [soccer_club] => xxxxx
        [soccer_team] => MP JO9-9
        [position] => keeper
        [printed] => 0
        [print_name] =>  
        [print_number] =>  
        [shirt_size] => 
        [sock_size] => 
        [pants_size] => 
        [referral] => Via de sportvereniging
        [comments] =>  
        [status] => paid
        [upsell] => 2 Shirt
    )

[50] => Array
    (
        [id] => 1468
        [transaction_id] => xxxxxxx
        [club] => xxxxxxxxxxx
        [event] => Voetbalmiddag
        [category] => Scoutingdagen
        [date_from] => 2017-05-31
        [date_end] => 2017-05-31
        [first_name] => xxxxx
        [last_name] => xxxxx
        [birth_date] => xxxxx
        [email] => x@gmail.com
        [address] => xxxx 35
        [zipcode] => xxxx
        [city] => xxxxx
        [phone] => xxxxx
        [soccer_club] => xxxx
        [soccer_team] => jo11-5
        [position] => field
        [printed] => 0
        [print_name] =>  
        [print_number] =>  
        [shirt_size] => 
        [sock_size] => 
        [pants_size] => 
        [referral] => Via de flyer
        [comments] =>  
        [status] => open
        [upsell] => 1 Shirt
    )

)

1 个答案:

答案 0 :(得分:0)

...
$first = true;
$results1 = $results;  
foreach($results1 as $result2){
...