How does PHP pass each element of an array as an argument to a mutable parameter function?

Source: Internet
Author: User
Real Development Encounters problems: How do I pass each element of an array as an argument to a mutable parameter function?

    1. From MySQL to find multiple data, for an array result set, eg: ['张三','李四', 'foo', 'bar'] ;

    2. The type that needs to be written to the result set Redis SET ;

    3. The Redis PHP Operation class Library provides sAdd() methods, using the following examples:

Examples of Use:

$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 '}*/

Because sAdd() it is a mutable parameter method, then the problem comes:
How to use the above ['张三','李四', 'foo', 'bar'] $redis->sAdd('key1' , '张三', '李四', 'foo', 'bar'); , instead of using the following loops:

$redis->multi (); foreach ([' Zhang San ', ' John Doe ', ' foo ', ' Bar '] as $value) {    $redis->sadd (' Key1 ', $value);} $redis->exec ();

Reply content:

Real development encounters a problem: how do I pass in an array of each element as an argument to a mutable parameter function?

    1. From MySQL to find multiple data, for an array result set, eg: ['张三','李四', 'foo', 'bar'] ;

    2. The type that needs to be written to the result set Redis SET ;

    3. The Redis PHP Operation class Library provides sAdd() methods, using the following examples:

Examples of Use:

$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 '}*/

Because sAdd() it is a mutable parameter method, then the problem comes:
How to use the above ['张三','李四', 'foo', 'bar'] $redis->sAdd('key1' , '张三', '李四', 'foo', 'bar'); , instead of using the following loops:

$redis->multi (); foreach ([' Zhang San ', ' John Doe ', ' foo ', ' Bar '] as $value) {    $redis->sadd (' Key1 ', $value);} $redis->exec ();
Call_user_func_array$redis->multi (); foreach ([' Zhang San ', ' John Doe ', ' foo ', ' Bar '] as $key = = $value) {$redis->sadd (' key ') . $key, $value);} $redis->exec ();

This is how PHP passes each element of an array as an argument to a mutable parameter function. For more information, please pay attention to topic.alibabacloud.com (www.php.cn)!

Related articles:

PHP variable parameters

The difference instance code between JS and PHP passing variable parameters to a function

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.