9

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);
Caconde
  • 4,177
  • 7
  • 35
  • 32
d1596
  • 1,187
  • 3
  • 11
  • 25
  • But wouldn't this defeat the whole purpose? – Dharman Jan 10 '19 at 23:22
  • You should probably use `array_filter`. What is your input and output? – ggorlen Jan 10 '19 at 23:25
  • Possible duplicate of [Jump out from array\_walk](https://stackoverflow.com/questions/20503625/jump-out-from-array-walk) – revo Jan 10 '19 at 23:25
  • @ggorlen input is class object of strings output is array of strings – d1596 Jan 10 '19 at 23:32
  • @d1596 It's helpful if you include that in your post. I recommend showing a [mcve], otherwise, this looks like an [x-y problem](https://en.wikipedia.org/wiki/XY_problem) because it appears like you're attempting to use a tool in a way it wasn't meant to be used. – ggorlen Jan 10 '19 at 23:41
  • thats what i assumed, but I wanted to be sure. So a foreach statement would be better? – d1596 Jan 10 '19 at 23:47
  • Sorry, I can't say because I'm not sure what problem you're trying to solve. – ggorlen Jan 10 '19 at 23:49

4 Answers4

12

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.

Ade
  • 2,961
  • 4
  • 30
  • 47
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.
});
Caconde
  • 4,177
  • 7
  • 35
  • 32
Clint
  • 973
  • 7
  • 18
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)

Vivick
  • 3,434
  • 2
  • 12
  • 25
  • I don't think that 2nd [return](http://php.net/manual/en/function.return.php) will happen. – Funk Forty Niner Jan 10 '19 at 23:28
  • 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. – Funk Forty Niner Jan 10 '19 at 23:29
  • 2
    What bothers me is that you're bugged with the `return` statements not the fact that neither `condition` nor `$transformedValue` is actually declared. – Vivick Jan 10 '19 at 23:31
  • "Bothers" you? huh? – Funk Forty Niner Jan 10 '19 at 23:32
  • @FunkFortyNiner omitting the braces on a single-statement block is bad practice, but it's permissible. – Sammitch Jan 11 '19 at 01:10
-1

You can try sth like this:

$newArrayWithIds = [];

array_map(function (int $id) use ($newArrayWithIds): void {
   if($id === 0) {
     return;
   }
   $newArrayWithIds[] = $id; 
}, $yourArrayWithIds);
Jankyz
  • 1,427
  • 1
  • 9
  • 8