The What
Description
The each
function does the following tasks:
- It copies the current element’s key and value pair out of the
$array_variable
. - It creates a new four element array and the assigns:
- the
$array_variable
key to both keys 0 and key - the
$array_variable
value to both keys 1 and value.
- the
- Then it moves the array’s internal pointer to the next element in the array.
- Then it returns the new array.
Parameters
array_variable
- The array to be worked on by
each
.
Return Values
It returns a four element array which has both the key (keyed by ‘0’ and ‘key’) and value (keyed by ‘1’ and ‘value’). It returns FALSE
when either the $array_variable is empty or it’s internal pointer is out-of-bounds and not pointing to a valid element.
Show It in ActionBasic
Let’s see the each
function in action. In this video, you will see how it works.
Once upon a time, there was a developer... You! This is going to be a good story...
Go DeepPro
In this video, you will go deeper into the instruction.
WhenPro
When do you use a each
versus a do/each
or foreach
?
Also See
The following PHP constructs were mentioned in the code examples: