I need to fill an array with a list of elements only if value is not null. Is there a way to skip the array filling if value to be appended is null?
As example:
$list = array();
for ($i = 1; $i <= 10; $i++) {
$modulo = ($i % 2);
if ($modulo) $list[] = $i;
}
Is there a way to write the two statement in the loop into an unique one without using $modulo variable?
something like...
$list = array();
for ($i = 1; $i <= 10; $i++) {
$list[] = ($i % 2);
}
The expected behavior is that $list array has to contain 1,3,5,7,9...
This is not the real example as the ($i % 2) has to be replaced with a complex function applied on an array of 380k elements and may return something or null. And I want to exclude null values.
1,3,5,7,9, so I don't get it.