Skip iteration of array_map function if IF statement True
Asked Answered
N

4

10

Is there a way to break or continue an iteration of the built-in method array_map() as you would in a normal for loop?

For example:

array_map(function (String s) {
    if (condition is met){
        continue;
    }
    return stuff;
}, $array_to_map);
Notus answered 10/1, 2019 at 23:18 Comment(7)
But wouldn't this defeat the whole purpose?Eudo
You should probably use array_filter. What is your input and output?Shawnee
Possible duplicate of Jump out from array_walkBury
@Shawnee input is class object of strings output is array of stringsNotus
@Notus It's helpful if you include that in your post. I recommend showing a minimal reproducible example, otherwise, this looks like an x-y problem because it appears like you're attempting to use a tool in a way it wasn't meant to be used.Shawnee
thats what i assumed, but I wanted to be sure. So a foreach statement would be better?Notus
Sorry, I can't say because I'm not sure what problem you're trying to solve.Shawnee
H
14

No. array_map returns an array the same length as the original so you can't skip an item. i.e. Something needs to be returned on each iteration.

You could use array_filter to remove certain items.

Hoyle answered 30/6, 2019 at 21:20 Comment(0)
O
4
$results = array_map(function (String s) {
    if (condition is met){
        //do stuff 
    } else {
        return false;
    }
    return stuff;
}, $array_to_map);

$results will then contain a array with the orignal number of elements in it as the $array_to_map, only with array elements set to false when the condition failed

then do.

$array_with_elements_remove = array_filter($results, function($e){
    return $e; //when this value is false the element is removed.
});
Occurrence answered 31/3, 2020 at 13:56 Comment(0)
G
0

You can emulate continue by simply returning the original value.

array_map(function($var){
  if(condition)
    return $var; //continue
  return $transformedValue;
}, $arr);

However there would be no way to actually break (except disgusting things like using a StopIteration exception class)

Guillen answered 10/1, 2019 at 23:26 Comment(5)
I don't think that 2nd return will happen.Metcalfe
Sure, but why post something like that? It's misleading. The manual on return states "If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. return also ends the execution of an eval() statement or script file." - If you get a downvote, it wouldn't have been mine, just saying.Metcalfe
What bothers me is that you're bugged with the return statements not the fact that neither condition nor $transformedValue is actually declared.Guillen
"Bothers" you? huh?Metcalfe
@FunkFortyNiner omitting the braces on a single-statement block is bad practice, but it's permissible.Dickinson
G
-1

You can try sth like this:

$newArrayWithIds = [];

array_map(function (int $id) use ($newArrayWithIds): void {
   if($id === 0) {
     return;
   }
   $newArrayWithIds[] = $id; 
}, $yourArrayWithIds);
Gilthead answered 29/11, 2021 at 7:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.