Reputation: 16204
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.
Upvotes: 48
Views: 200763
Reputation: 931
I found Delcon answer helpful but I was looking for this
function sendmail($user1, $user2, $user3){
echo $user1;
echo $user2;
echo $user3;
}
$users = array('user1','user2','user3');
sendmail(...$users);
Upvotes: 3
Reputation: 73
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);
}
?>
Upvotes: 2
Reputation: 71
<?php
function takes_array($input)
{
echo "$input[0] + $input[1] = ", $input[0]+$input[1];
}
?>
Upvotes: 0
Reputation: 2555
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');
Upvotes: 15
Reputation: 490283
You can pass an array as an argument. It is copied by value (or COW'd, which essentially means the same to you), so you can array_pop()
(and similar) all you like on it and won't affect anything outside.
function sendemail($id, $userid){
// ...
}
sendemail(array('a', 'b', 'c'), 10);
You can in fact only accept an array there by placing its type in the function's argument signature...
function sendemail(array $id, $userid){
// ...
}
You can also call the function with its arguments as an array...
call_user_func_array('sendemail', array('argument1', 'argument2'));
Upvotes: 70
Reputation: 11
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/
Upvotes: 1
Reputation: 706
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);
Upvotes: 5
Reputation: 4067
Its no different to any other variable, e.g.
function sendemail($id,$userid){
echo $arr["foo"];
}
$arr = array("foo" => "bar");
sendemail($arr, $userid);
Upvotes: 2
Reputation: 577
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.
Upvotes: 1
Reputation: 13003
In php 5, you can also hint the type of the passed variable:
function sendemail(array $id, $userid){
//function body
}
See type hinting.
Upvotes: 1
Reputation: 28755
function sendemail(Array $id,$userid){ // forces $id must be an array
Some Process....
}
$ids = array(121,122,123);
sendmail($ids, $userId);
Upvotes: 4
Reputation: 86406
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);
Upvotes: 4