emco
emco

Reputation: 4719

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

I have this multidimensional array (called $values):

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.

Upvotes: 0

Views: 333

Answers (3)

Matthew
Matthew

Reputation: 48314

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.

Upvotes: 5

deceze
deceze

Reputation: 522636

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.

Upvotes: 0

Kirby Todd
Kirby Todd

Reputation: 11566

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

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

Upvotes: 0

Related Questions