I have a function which I have to loop it a few times but I'm getting an error of Cannot redeclare function(), is it not possible to loop a function like this, or is it just a problem in my function,
Thanks for your help.
while($i=0){
function gbFunc1($elem){ return (string)$elem['companyCode'][0]; }
function gbFunc2($elem){ return (string)$elem['locationType'][0][0]->locationDescription->name; }
function gbFunc3($elem){ return (string)$elem['vehicleRentalPrefType'][0]; }
function gbFunc4($elem){ return (string)$elem['customerReferenceInfo'][0]->referenceQualifier; }
function gbFunc5($elem){ return (string)$elem['customerReferenceInfo'][0]->referenceNumber; }
function vFunc3($elem){ return (float)$elem['rateAmount'][0][1]->rateAmount; }
$new = Fx::GroupBy($results_array, array('func:gbFunc1', 'func:gbFunc2', 'func:gbFunc3', 'func:gbFunc4', 'func:gbFunc5'), array(null, null, null, null, 'func:vFunc3',));
}
You need to declare your functions outside of the loop, otherwise each time the loop iterates it will re-declare the functions again.
function gbFunc1($elem){ return (string)$elem['companyCode'][0]; }
function gbFunc2($elem){ return (string)$elem['locationType'][0][0]->locationDescription->name; }
function gbFunc3($elem){ return (string)$elem['vehicleRentalPrefType'][0]; }
function gbFunc4($elem){ return (string)$elem['customerReferenceInfo'][0]->referenceQualifier; }
function gbFunc5($elem){ return (string)$elem['customerReferenceInfo'][0]->referenceNumber; }
function vFunc3($elem){ return (float)$elem['rateAmount'][0][1]->rateAmount; }
while($i=0){
$gbF1 = gbFunc1($elem);
$gbF2 = gbFunc2($elem);
$gbF3 = gbFunc3($elem);
$gbF4 = gbFunc4($elem);
$gbF5 = gbFunc5($elem);
$vF3 = vFunc3($elem);
$new = Fx::GroupBy($results_array, array($gbF1, $gbF2, $gbF3, $gbF4, $gbF5), array(null, null, null, null, $vF3,));
}
Here are two snippets:
1)
for($i=0; $i<10; $i++)
{
function squared($a)
{
return($a * $a);
}
$foo[$i] = squared($bar[$i]);
}
2)
function squared($a)
{
return($a * $a);
}
for($i=0; $i<10; $i++)
{
$foo[$i] = squared($bar[$i]);
}
Snippet 1 will generate the "cannot redeclare..." error. Since, on every iteration you're redeclaring your function squared()
.
Snippet 2 only declares the squared()
function once.
Both snippets call the function 10 times, the difference is in how many times it is declared -- this can only happen once.
Or simply:
function groupBy($elem){
return array('companyCode'=>$elem['companyCode'][0],
'locationType'=>$elem['locationType'][0][0]->locationDescription->name,
'vehicleRentalPrefType'=>$elem['vehicleRentalPrefType'][0],
'referenceQualifier'=>$elem['customerReferenceInfo'][0]->referenceQualifier,
'referenceNumber'=>$elem['customerReferenceInfo'][0]->referenceNumber,
'rateAmount'=>$elem['rateAmount'][0][1]->rateAmount);
}
$gb=groupBy($elem);
while($i=0){
$new = Fx::GroupBy($results_array, array($gb['companyCode'],
$gb['locationType'],
$gb['vehicleRentalPrefType'],
$gb['referenceQualifier'],
$gb['referenceNumber'],
$gb['rateAmount']),
array(null, null, null, null, $gb['rateAmount']));
}