I have an array of arrays. They are all rows of people that subscribed for a seminar. At the end of the table there are some upsell products added. Problem is: sometimes someone orders for 2 people and the upsell products, that belong to a transaction, not to a person are joined behind both persons. So If I sign up 2 people and buy 2 shirts the list states 2 shirts twice.
So I thought: let's loop through it, see if I find some upsells and then remove every second occurence with a simple double foreach
. But the value of $first
remains true
always. While it is false
if I echo it below the declaration. I echoed the values several times and I added the value in comments the code.
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 weird scope thing going on here? In my opinion it should loop first and be true then loop second and be false.
EDIT, added the print_r
of 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
)
)
...
$first = true;
$results1 = $results;
foreach($results1 as $result2){
...