PHP动态参数

WBOY
Release: 2016-06-06 20:17:24
Original
1731 people have browsed it

$result = [
    'a'=>[1,2],
    'b'=>[1,2],
    'c'=>[1,2],
    'd'=>[1,2],
]
array_intersect_ukey(
                    $result['a'],
                    $result['b'],
                    $result['c'],
                    $result['d'],
                    'test'
                    );
Copy after login
Copy after login

现在的问题是,我不知道$result里面有多少数组,有可能a-d,有可能只有a和d。

那array_intersect_ukey函数也要动态根据此动态的传入参数。
比如只有a和d

array_intersect_ukey(
                    $result['a'],
                    $result['d'],
                    'test'
                    );
Copy after login
Copy after login

比如只有a,c和d

array_intersect_ukey(
                    $result['a'],
                    $result['c'],
                    $result['d'],
                    'test'
                    );
Copy after login
Copy after login

要如何实现这种动态传入参数呢?或者有没有其他的实现方法?

回复内容:

$result = [
    'a'=>[1,2],
    'b'=>[1,2],
    'c'=>[1,2],
    'd'=>[1,2],
]
array_intersect_ukey(
                    $result['a'],
                    $result['b'],
                    $result['c'],
                    $result['d'],
                    'test'
                    );
Copy after login
Copy after login

现在的问题是,我不知道$result里面有多少数组,有可能a-d,有可能只有a和d。

那array_intersect_ukey函数也要动态根据此动态的传入参数。
比如只有a和d

array_intersect_ukey(
                    $result['a'],
                    $result['d'],
                    'test'
                    );
Copy after login
Copy after login

比如只有a,c和d

array_intersect_ukey(
                    $result['a'],
                    $result['c'],
                    $result['d'],
                    'test'
                    );
Copy after login
Copy after login

要如何实现这种动态传入参数呢?或者有没有其他的实现方法?

我没怎么细看楼主的问题,不过我告诉楼主两个点,我敢肯定是能解决楼主所描述的问题:

1.留意一个叫做call_user_func_array()的函数
http://php.net/manual/zh/function.call-user-func-array.php
2.这个可能不如1重要,但是还要提一下吧:
func_num_args(); //输出参数个数
func_get_args; //获取参数,返回参数数组
3.既然参数个数以及名称不确定,那么,可以通过1中函数向某个函数传参,记得参数用数组方式,数组可以让你传各种各样的参数

楼主的问题,应该可以的到解决.

回来又仔细看了一下楼主的问题,没错,call_user_func_array可以是帮助你。

可以递归或循环调用。

根据A ∩ B ∩ C = ( A ∩ B ) ∩ C,第一次计算头两个数组的交集,然再用第一次计算的结果和第三个数组(如果有的话)计算交集,以此类推。

我以前问过类似的问题 https://segmentfault.com/q/1010000004290664

Related labels:
php
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!