Answer the question
In order to leave comments, you need to log in
Which code option is better?
The question of an ideological nature arose as to how to write correctly. So:
function a($data){
$result=[];
foreach($data as $item){
if($item==1){
$result[]=$item;
}
}
return $result;
}
function a($data){
$result=[];
foreach($data as $item){
if($item!=1){
continue;
}
$result[]=$item;
}
return $result;
}
Answer the question
In order to leave comments, you need to log in
php.net/manual/en/function.array-filter.php
$data = array_filter($data, function ($item) {
return ($item == 1);
});
The first option, of course, is shorter and more readable (just for this case, but otherwise it depends on the code)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question