Perry J
Perry J

Reputation: 365

PHP: How to pass array as argument to receiving function that uses func_get_args?

I'm having a problem with getting an array (with all its data) over to a receiving function. I am passing the array over as a constructor argument ($myarray):

$s = new MyQuery($param1, $myarray);

The receiving side is a MyQuery object receiving the arguments, using:

$a = func_get_args();  

But it does not give me the values in the array: If I do:

$size=func_num_args();
$a=func_get_args();
for ($i=0;$i<$size;$i++) {
    if (is_array($a[$i])){
        $arr = $a[$i]; //trying to get the very array....
        echo ($arr[0]);
    }
}

.. the echo here does just say "Array". Does it have to do with the func_get_args() function? Very thankful for any help.

Upvotes: 0

Views: 86

Answers (2)

azibom
azibom

Reputation: 1944

Try this code:

<?php

function foo()
{
    $argsCount = func_num_args();
    $args=func_get_args();
    for ($i = 0; $i < $argsCount ; $i++) {
        if (is_array($args[$i])){
            print_r($args[$i]);
        }
    }
}

foo(1, 2, [3]);   
?>

output

Array
(
    [0] => 3
)

Upvotes: 1

mbesson
mbesson

Reputation: 749

Actually you should be able to get you array with this piece of code

But echo can't print the full array.

Try replacing echo ($arr[0]); by var_dump($arr); or print_r($arr);

Upvotes: 0

Related Questions