Say I have this array
$array = array('pen' => 'blue', 'paper' => 'red', 'ink' => 'white');
When I loop through it
$string = '';
foreach ($array AS $key=>$value) {
$string .= $key . ' = ' . $value;
}
I want to get the “line number” of the element the loop is currently on.
If the loop is on “pen” I would get 1.
If the loop is on “paper” I would get 2.
If the loop is on “ink” I would get 3.
Is there an array command for this?
No. You will have to increment an index counter manually: