Without further context, what you did was
What you really should do is KISS and DRY. Make the code easy to read and understand, optimize it for computation time and remove complex structures, such as loops. Just as an example: Loops in your code have to be interpreted, while using built-in methods, which loop over iterable structures, run on the hardware directly.
So, taking your first code snippet, an optimization would look like this:
function processProducts($product, $key) {
// you might want to put your process code in this function
// or split it up in some other way
process($product);
if (somecondition) {
doSomething($product);
}
}
array_walk($products, 'processProducts');
Depending on your PHP architecture and real benchmarks, that approach might be disruptive, so you have to decide if it is worth implementing.