开发者

converting array of values to an array of references

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


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.


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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜