How to pass array by reference to function and change the array values in the function in PHP

1 Answer

0 votes
function potatoArray(&$array)
{
    $array[0] = 'xyz';
}

$arr = array(
    'aaa',
    'bbb',
    'ccc'
);

potatoArray($arr);

echo "<pre>";
print_r($arr); 
echo "</pre>";

 
/*
run: 

Array
(
    [0] => xyz
    [1] => bbb
    [2] => ccc
)

*/

 



answered May 16, 2016 by avibootz

Related questions

1 answer 183 views
1 answer 170 views
2 answers 242 views
1 answer 121 views
1 answer 116 views
...