php push to array from function
231
Nice day for all!
I need to fill the "global" array with other arrays. As an example, I wrote this function:
<?php
function f($ar=array(), $gl=array()){
$gl[0]=array($ar[1]);
}
$globals=array();
$array_example=array('foo', 'bar');
f($array_example, $globals);
print_r($globals); //$globals is empty!
?>
Help me please. I can not understand why the var $globals is empty.
Thanks!
Answer
Solution:
I think you should pass the array by reference using
&
:Will give you:
Demo
Answer
Solution:
man first you should modify the
function
to return the value and assign the returned value for the value to be printed . you are print the$global
and it is an emptyarray
. so your code should be like this .Answer
Solution:
Function always works on locally variables, And you want to use their locally variable scope to out of function. Check with below code, I hope it will help you