If you need both the value and its offset or key while looping through an array in PHP, use foreach with both variables.

1
2
3
4
5
6
7
8
9
$fruits = array(
    0 => "apple",
    1 => "orange",
    2 => "lemon"
);

foreach ($fruits as $key => $fruit) {
    printf("%s is in index %s\n", $fruit, $key);
}

That gives you the array value in $fruit and the corresponding index or key in $key.