Avatar ·

How to get element index in foreach loop in PHP?

Hi everyone! I started learning PHP and encountered this problem. I have an array and I want to iterate through it using a foreach loop, but I need to know not only the element value but also its ordinal number (index). For example, I have a list of products: ```php $products = ['apple', 'banana', 'cherry', 'date']; ``` I want to output them as a numbered list: 1. apple 2. banana 3. cherry 4. date I tried using a regular for loop with a counter $i, but it's inconvenient when the array is associative or when the keys are not numeric. The documentation says that foreach works with keys, but I don't understand how to get them. Please tell me how to correctly get the index (or key) inside a foreach loop? I need a solution for both regular numeric arrays and associative arrays where the keys are