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 191 views
1 answer 175 views
2 answers 249 views
1 answer 128 views
1 answer 123 views
...