Home > Backend Development > PHP Tutorial > How to pass each element of an array as an actual parameter of a variable parameter function in PHP?

How to pass each element of an array as an actual parameter of a variable parameter function in PHP?

PHP中文网
Release: 2023-03-01 08:16:02
Original
3034 people have browsed it

Problems encountered in real development: How to pass each element of the array as an actual parameter of the variable parameter function?

  1. Multiple pieces of data are found from MySQL, which is an array result set, eg: ['Zhang San','Li Si', 'foo', 'bar'];

  2. Need to be The result set is written to the SET type of Redis; the PHP operation class library of

  3. Redis provides the sAdd() method. The usage examples are as follows:

Usage examples:

$redis->sAdd('key1' , 'member1'); /* 1, 'key1' => {'member1'} */
$redis->sAdd('key1' , 'member2', 'member3'); /* 2, 'key1' => {'member1', 'member2', 'member3'}*/
$redis->sAdd('key1' , 'member2'); /* 0, 'key1' => {'member1', 'member2', 'member3'}*/
Copy after login
Copy after login

because sAdd() is a variable parameter method, so the question is:
How can we use the above ['Zhang San', 'Li Si', 'foo', 'bar'] like this $redis->sAdd ('key1', 'Zhang San', 'Li Si', 'foo', 'bar'); instead of using it in a loop as follows:

$redis->multi();
foreach(['张三','李四', 'foo', 'bar'] as $value) {
    $redis->sAdd('key1', $value);
}
$redis->exec();
Copy after login
Copy after login

Reply content:

Real development problems: How to use arrays Each element is passed in as an argument to a variadic function?

  1. Multiple pieces of data are found from MySQL, which is an array result set, eg: ['Zhang San','Li Si', 'foo', 'bar'];

  2. The results need to be Sets are written to the SET type of Redis; the PHP operation class library of

  3. Redis provides the sAdd() method. The usage examples are as follows:

Usage examples:

$redis->sAdd('key1' , 'member1'); /* 1, 'key1' => {'member1'} */
$redis->sAdd('key1' , 'member2', 'member3'); /* 2, 'key1' => {'member1', 'member2', 'member3'}*/
$redis->sAdd('key1' , 'member2'); /* 0, 'key1' => {'member1', 'member2', 'member3'}*/
Copy after login
Copy after login

because sAdd() is a variable parameter method, so the question is:
How can we use the above ['Zhang San','Li Si', 'foo', 'bar'] like this$redis->sAdd( 'key1', 'Zhang San', 'Li Si', 'foo', 'bar'); instead of using it in a loop as follows:

$redis->multi();
foreach(['张三','李四', 'foo', 'bar'] as $value) {
    $redis->sAdd('key1', $value);
}
$redis->exec();
Copy after login
Copy after login
call_user_func_array
$redis->multi();foreach(['张三','李四', 'foo', 'bar'] as $key=>$value) {
$redis->sAdd('key'.$key, $value);
}$redis->exec();
Copy after login

The above is how PHP uses each element of the array as a variable parameter function Actual parameters passed in? For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!

Related articles:

php variable parameters

The difference between JS and PHP in passing variable parameters to functions Example code

php variable parameter implementation

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template