I want to do something like this:
foreach ($array as $key=>$value except when $key="id")
{
// whatever
}
... without having to put an "if" clause inside the body of the loop. It is not guaranteed that "id" will the be the first or last element in the array, and I don't really want to unset or slice the array, because that will be expensive, ugly, and not maintain the original data. I also definitely need to use both key and value inside the loop.
Any ideas?
I don't think that the if-clause is such a problem:
foreach ($array as $key => $value) {
if ($key == 'ignore_me') continue;
if ($key == 'ignore_me_2') continue;
If you want a fancy solution, you can use array_diff_key
:
$loop_array = array_diff_key($actual_array, array('ignore_me' => NULL, 'ignore_me_2' => NULL));
foreach ($loop_array as $key => $value) {
#...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With