[php] Can we pass an array as parameter in any function in PHP?

I have a function to send mail to users and I want to pass one of its parameter as an array of ids.

Is this possible to do? If yes, how can it be done?

Suppose we have a function as:

function sendemail($id, $userid) {

}

In the example, $id should be an array.

This question is related to php arrays function

The answer is


Yes, you can do that.

function sendemail($id_list,$userid){
    foreach($id_list as $id) {
        printf("$id\n"); // Will run twice, once outputting id1, then id2
    }
}

$idl = Array("id1", "id2");
$uid = "userID";
sendemail($idl, $uid);

even more cool, you can pass a variable count of parameters to a function like this:

function sendmail(...$users){
   foreach($users as $user){

   }
}

sendmail('user1','user2','user3');

What should be clarified here.

Just pass the array when you call this function.

function sendemail($id,$userid){
Some Process....
}
$id=array(1,2);
sendmail($id,$userid);

Its no different to any other variable, e.g.

function sendemail($id,$userid){
  echo $arr["foo"]; 
}

$arr = array("foo" => "bar");
sendemail($arr, $userid);

Since PHP is dynamically weakly typed, you can pass any variable to the function and the function will try to do its best with it.

Therefore, you can indeed pass arrays as parameters.



Yes, we can pass arrays to a function.

$arr = array(“a” => “first”, “b” => “second”, “c” => “third”);

function user_defined($item, $key)
{
    echo $key.”-”.$item.”<br/>”;
} 

array_walk($arr, ‘user_defined’);

We can find more array functions here

http://skillrow.com/array-functions-in-php-part1/


In php 5, you can also hint the type of the passed variable:

function sendemail(array $id, $userid){
  //function body
}

See type hinting.


<?php

function takes_array($input)

{

    echo "$input[0] + $input[1] = ", $input[0]+$input[1];

}

?>

function sendemail(Array $id,$userid){  // forces $id must be an array
Some Process....
}


$ids  = array(121,122,123);
sendmail($ids, $userId);

I composed this code as an example. Hope the idea works!

<?php
$friends = array('Robert', 'Louis', 'Ferdinand');
  function greetings($friends){
    echo "Greetings, $friends <br>";
  }
  foreach ($friends as $friend) {
  greetings($friend);
  }
?>

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to function

$http.get(...).success is not a function Function to calculate R2 (R-squared) in R How to Call a Function inside a Render in React/Jsx How does Python return multiple values from a function? Default optional parameter in Swift function How to have multiple conditions for one if statement in python Uncaught TypeError: .indexOf is not a function Proper use of const for defining functions in JavaScript Run php function on button click includes() not working in all browsers