Boolean in nested foreach scope gives unexpected value

I have an array of arrays. These are all the ranks of people who signed up for the seminar. Added some upsell products at the end of the table. The problem is that sometimes someone orders 2 people, and high income products that are transactional, not person related, are pooled for both people. So if I register 2 people and buy 2 shirts, the list shows 2 shirts twice.

So I thought, let's go through it, see if I find any highs, and then delete every second occurrence with a simple double foreach

. But the meaning $first

always remains true

. For now false

, if I repeat it below the announcement. I repeated the values ​​several times and I added the value in the code comments.

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

      

Is there some strange scope? In my opinion, it should first loop and be true and then second and be false.

EDIT, added print_r

results.

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
    )

)

      

+3


source to share


1 answer


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

      



0


source







All Articles