开发者

How to pass a variable number of parameters to a function in PHP

开发者 https://www.devze.com 2023-02-18 23:12 出处:网络
I have this multidimensional array (called $values): 开发者_C百科Array ( [0] => Array ( [0] => 5

I have this multidimensional array (called $values):

开发者_C百科Array
(
    [0] => Array
        (
            [0] => 5
            [1] => 2
            [2] => 5
            [3] => 6
        )

    [1] => Array
        (
            [0] => 3
            [1] => 4
            [2] => 5
            [3] => 6
        )

    [2] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 4
            [3] => 5
        )

    [3] => Array
        (
            [0] => 9
            [1] => 5
            [2] => 3
            [3] => 2
        )
)

I want to calculate the diff between every element (array) of this multidimensional array using array_diff PHP function. The first thing I've thought is to split the multidimensional array into single arrays with this:

for($cnt = 0; $cnt < count($values); $cnt++){
        for($cntB = 0; $cntB < 4; $cntB++){
            ${'arr'.$cnt}[] = $values[$cnt][$cntB];
        }
    }

After this I have several arrays called $arr1, $arr2, and so on. Since the dimension of the array $values may vary (and it will) I can't find a way to pass all the generated single arrays to the function array_diff,

Any thoughts?

Thanks in advance.


Not sure if this is what you want, as I didn't read all of that, but check out:

call_user_func_array('array_diff', $values)

Maybe that's what you want.


function diff() {
    $args = func_get_args();

    // $args how has all the arrays you passed in.
}


Instead of

${'arr'.$cnt}[] = ...

use

$arr[$cnt][] = ...

Problem solved. :)

There's no need for variable variables when what you're really looking for is an array.

0

精彩评论

暂无评论...
验证码 换一张
取 消