in reply to How do I find the array element directly preceeding a 'match'?

The "correct" solution is to iterate through the indexes, not the elements themselves:
for ($i = 0; $i < @array; $i++) { if (matches($array[$i])) { $found = $array[$i-1]; } }
If you want to insist doing it in foreach style:
foreach $item (@array) { if (matches($item)) { $found = $last; } $last = $item; }

Originally posted as a Categorized Answer.