Reputation: 13
Im trying to build an array that needs to be passed to a report. Some of the data returned has similar field names so im using the function below to add a prefix to the array key names before merging the arrays, however i get an out of memory exception "Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 44 bytes) in ..", is there another way of adding prefix to array keys in an array that will not use alot of memory?
function prefixArrayKeys(&$_array,$prefix){
foreach($_array as $k=>$v){
$nk = $prefix.$k;
$nv = $v;
array_push($_array, array($nk=>$nv));
unset($_array[$k]);
}
var_dump($_array);
}
The call to the function:
$aSQL = "select sex, a_number, to_char(b_dtm, 'DD/MM/YYYY') b_dtm from atable where a_id = ".$ped_array[1]['D']."";
execute_sql($aSQL,$rsGTYPE);
prefixArrayKeys(&$rsGTYPE[0],"D");
if(count($rsGTYPE) > 0) $rowdata[0] = array_merge($rowdata[0],$rsGTYPE[0]);
Upvotes: 1
Views: 2342
Reputation: 1046
Within your foreach loop, you are using array_push.
You are adding to the array you are iterating through, this is an infinite loop.
Upvotes: 2
Reputation: 196
May be if you try to unset your $_array[$k] before to set the new one would makes it work? Although I doubt that changes anything, except if the entry in huge...
Upvotes: 0