-1

I have below code. But I cannot understand what is meant by $seat_key => $seat in foreach. In other terms, I understand foreach ($seats as $seat), but not foreach ($seats as $seat_key => $seat)

foreach ($seats as $seat_key => $seat) {

    $email_found = false;

    foreach ($email_accounts as $key => $email_account) {

        if (strtolower($email_account->email) == strtolower($seat->email) && empty($email_account->deactivated_since) && empty($email_account->flg_deleted)) {
            unset($seats[$seat_key]);
            unset($email_accounts[$key]);
            $email_found = true;

            if (isset($email_found_seats[$seat->email])) {
                $email_duplicated_seats[] = $seat;
            } else {
                $email_found_seats[$seat->email] = $seat;
            }
        }
    }
}
Pradeep Sanjeewa
  • 636
  • 5
  • 18
  • 1
    Does this answer your question? [PHP equals arrow operator in a foreach loop](https://stackoverflow.com/questions/7732013/php-equals-arrow-operator-in-a-foreach-loop) – El_Vanja May 04 '21 at 16:41

1 Answers1

2

The foreach construct when used in this way will allow you to obtain both the key and the value of the item when enumerating the array or object.

In the case of the example, the statement is:

foreach ($seats as $seat_key => $seat)

This means that for each enumerated item, the key of the item will be stored in $seat_key and the value of the item will be stored in $seat.

The PHP manual has a great, detailed explanation of this but for reference the function declaration in the manual is shown as:

foreach (iterable_expression as $key => $value)
    statement
Martin
  • 14,189
  • 1
  • 26
  • 43