How does php pass in each element of the array as the real parameter of the variable parameter function?

Source: Internet
Author: User
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?

  1. Multiple data records are retrieved from MySQL, which is an array result set. for example:['Zhang San', 'Li Si', 'foo', 'bar'];

  2. Write the result setRedisOfSETType;

  3. 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?

  1. Multiple data records are retrieved from MySQL, which is an array result set. for example:['Zhang San', 'Li Si', 'foo', 'bar'];

  2. Write the result setRedisOfSETType;

  3. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.