1

I want the foreach to return arrays but it just returns a single array. But the array_map with same code does.

  • What is the correct way of getting the arrays out of the foreach.
  • Why does foreach behaves differently than array_map.

Inside the file (userdata.php)

Reme:[email protected]
george bush:[email protected]
obama:[email protected]

Using array_map

function registered_users(){
 $file_user = file('userdata.php');
    return array_map(function($user){
        return explode(':',$user);
    },$file_user);
    } //returns the exploded array correctly.

Using foreach

function registered_users(){
     $file_user = file('userdata.php');
        foreach ($file_user as $user) {
            return explode(':',$user);
    }
 }// returns Array ( [0] => Reme [1] => [email protected] ) 
3
  • foreach isn't a function that returns a value, it's a language construct. You can't use it like that (or rather you can, but returning within a foreach terminates it prematurely which may or may not be what you want). Commented May 8, 2015 at 9:49
  • @GordonM Even I have used a return in the array_map why is it not moving out ? Commented May 8, 2015 at 12:38
  • You're not using a return in an array_map. You're using a return in the callback function that array_map is executing. It's an important distinction. Commented May 8, 2015 at 13:41

1 Answer 1

3

Because array_map() iterates over all elements in the array.... the foreach() would do the same except that your return is jumping out of it on the first iteration.

function registered_users(){
    $users = [];
    $file_user = file('userdata.php');
    foreach ($file_user as $user) {
        $users[] = explode(':',$user);
    }
    return $users;
}

EDIT

In response to your question "Why doesn't a return from array_map terminate the iteration?"

Because array_map() is a function that loops/iterates every element in the array, executing a "callback" function against each element. Your return is in the "callback" function, which acts on one individual array element at a time, and is called multiple times by array_map(), once for each element of the array in turn.

The return in your "callback" is simply returning a modified value for that one individual element (the current element in the array_map() loop) to the array_map() function.... it's telling array_map() what the new element value should be.

The array_map() function itself can't be interrupted: it will then continue iterating over the next element, sending that in turn to the "callback" function until it has done so for every element in the array.

Sign up to request clarification or add additional context in comments.

1 Comment

Even I have used a return in the array_map why is it not moving out ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.