Real Development encounters a problem: how to pass in each element of the array as the real parameter of the variable parameter function? Multiple data records are retrieved from MySQL, which is an array result SET, for example: [Zhang San, Li Si, foo, bar]; the result SET needs to be written to the SET type of Redis; PHP operation class library for Redis... problems encountered during real development:
How to pass in each element of the array as the real parameter of the variable parameter function?
Multiple data records are retrieved from MySQL, which is an array result set. for example:['Zhang San', 'Li Si', 'foo', 'bar']
;
Write the result setRedis
OfSET
Type;
Redis PHP operation class library providessAdd()
Example:
Example:
$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'}*/
BecausesAdd()
Is a variable parameter method, so the problem is:
How can I['Zhang San', 'Li Si', 'foo', 'bar']
Use this method$ Redis-> sAdd ('key1', 'Zhang San', 'Li Si', 'foo', 'bar ');
Instead of repeating the following steps:
$ Redis-> multi (); foreach (['Zhang San', 'Li Si', 'foo', 'bar'] as $ value) {$ redis-> sAdd ('key1', $ value) ;}$ redis-> exec ();
Reply content:
Problems encountered during real development:How to pass in each element of the array as the real parameter of the variable parameter function?
Multiple data records are retrieved from MySQL, which is an array result set. for example:['Zhang San', 'Li Si', 'foo', 'bar']
;
Write the result setRedis
OfSET
Type;
Redis PHP operation class library providessAdd()
Example:
Example:
$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'}*/
BecausesAdd()
Is a variable parameter method, so the problem is:
How can I['Zhang San', 'Li Si', 'foo', 'bar']
Use this method$ Redis-> sAdd ('key1', 'Zhang San', 'Li Si', 'foo', 'bar ');
Instead of repeating the following steps:
$ Redis-> multi (); foreach (['Zhang San', 'Li Si', 'foo', 'bar'] as $ value) {$ redis-> sAdd ('key1', $ value) ;}$ redis-> exec ();
Call_user_func_array $ redis-> multi (); foreach (['Zhang San', 'Li Si', 'foo', 'bar'] as $ key => $ value) {$ redis-> sAdd ('key '. $ key, $ value) ;}$ redis-> exec ();
The above is how php passes in each element of the array as the real parameter of the variable parameter function? For more information, see The PHP Chinese website (www.php1.cn )!
Related articles:
Php variable parameters
Differences between passing variable parameters to functions in JS and PHP instance code
Php variable parameter implementation