3

When I use print_r to examine the contents of this function $arr and $refs are the same.

Odd because this was a solution given here to the problems of passing call_user_func_array an array of references.

Does this function return an array of references or an array of values?

function makeValuesReferenced($arr){ 
    $refs = array(); 
    foreach($arr as $key => $value) 
        $refs[$key] = &$arr[$key]; 
    return $refs; 

}

Function Call:

print_r($db_->ref_arr(array(1,2,3,4)));

Results

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

Info. on prepared statements here

Info. on call_user_func_array here

Info. on the neccessity of references for call_user_func_array here

Does this function return an array of references or an array of values?

Update: using var_dump and adding & to parameter gives similar results...adds verification that ints are being returned.

1 2 3 4 array(4) { [0]=> &int(1) 1=> &int(2) 2=> &int(3) 3=> &int(4) }

4
  • 1
    change the values in arr and see if the values in ref change Commented Sep 12, 2011 at 2:16
  • I'm not sure that referenced and value variables will look any different. What is it that you're trying to accomplish? Commented Sep 12, 2011 at 2:20
  • Could you please rephrase your question in the form of a question? Commented Sep 12, 2011 at 2:34
  • If the memory chip comment was aimed at me, I'm not sure what it was meant to represent. What are you trying to do? Commented Sep 12, 2011 at 2:36

2 Answers 2

4

No, your function does not return an array of references. If you want to return an array of references, change to:

function makeValuesReferenced(&$arr){ 
    $refs = array(); 
    foreach($arr as $key => $value) 
        $refs[$key] = &$arr[$key]; 
    return $refs; 

}

PS: You should use var_dump to check.

Sign up to request clarification or add additional context in comments.

2 Comments

This is called Passing by Reference.
print_r only print out the value, var_dump can show you whether it is a reference.
0

There is an alternative way of doing this that involves a helper function.

//Helper function
function makeValueReferenced(&$value) {
    return $value;
}

function makeValuesReferenced($array) {
    return array_map("makeValueReferenced", $array);
}

Comments

Your Answer

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