Skip to content
Advertisement

Return Array values from PHP Function

I’m trying to get the users from PHP form. PHP Function has to create new users (username, first name & email) with registeruser($user),that is to insert to database.

I was able to insert only one row to database. Could you guide me to insert more than one user and return all the users.

The new users come from $array['newuser'].

function newusers() {
    foreach($array['newuser'] as $row) {
            
        $user = new stdClass();
        $user->username  = $row['username'];
        $user->firstname  = $row['firstname'];
        $user->email = $row['email'];
    
        $userreg = registeruser($user);
        return array($userreg)
    }
}

Advertisement

Answer

i think you are searching for this answer;here whenever new user comes it will store to an array $userreg[] =registeruser($user); and return that array after the loop

function newusers($array_of_new_users) {
        $userreg=array();
        foreach($array_of_new_users as $row) 
        {
                
            $user = new stdClass();
            $user->username  = $row['username'];
            $user->firstname  = $row['firstname'];
            $user->email = $row['email'];
            $userreg[] = registeruser($user);
         }
    return $userreg;
            
    }

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement