0

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.

2
  • The printed string "Array" indicates that $arr[0] is an array itself. Try var_dump($arr[0]); instead to better understand what you're accessing. Commented Jan 19, 2021 at 10:12
  • Thank you very much! var_dump ($a[$i]) gives: array(1) { [0]=> array(1) { [0]=> string(3) "cat" } } Now I am one step further! How to access "cat" directly? Commented Jan 19, 2021 at 10:35

2 Answers 2

1

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
)
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much! At least, by using var_dump and print_r, I see the array data is actually there. But I cannot use it like that.I must be able to get at that value directly, what is the syntax? var_dump ($a[$i]) gives: array(1) { [0]=> array(1) { [0]=> string(3) "cat" } } I cant use var_dump in my real app. I want to get "cat" as a value to use.
try this $a[$i][0][0]
SPLENDIDO!!!!! Great! Thank you very much for help!
You're welcome, if my answer helped you with your question, I would appreciate it if you upvote my answer and choose it as the best answer as you wish.
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.