I am sending a reference of an array variable which is initially empty.
In the called function the array gets populated.
function one()
{
$ret = array();
two($ret);
pri nt_r($ret);
}
function two(&$res)
{
foreach($a as $b)
{
$id = $b->getid();
$txt = $b->gettxt();
$res[$id] = $txt;
}
}
Here, if $id is duplicated i assume that it is by default overwritten.
That is if the foreach runs for 5 times and for three times if id=5 then the result is only two elements in the array;
Is this the default behavior for this kind of assignment of arrays?
or am i missing something?
Yes, it is overwriting the duplicates with the last values. I tested separately and it happened to be as assumed. But if we use array_push i hope it will be duplicated. I thought the direct assignment of duplicates will be duplicated instead of overwriting. anyway let me get some comments form the users so i can update if i am missing some useful information about this duplicating and overwriting.
From the PHP Manual on Arrays:
Further
And most important for your question