I have an array ($cart) that I want to process in a foreach loop either in reverse order or in normal order depending on an external switch. The code below gives the idea of what I am trying to do,
switch ($data['entire_receipt'])
{
case 'N':
$foreach = 'array_reverse($cart, true) as $line=>$item';
break;
case 'Y':
$foreach = '$cart as $line=>$item';
break;
default:
$foreach = 'array_reverse($cart, true) as $line=>$item';
}
//foreach $$foreach
foreach ("$foreach")
{
// do something
}
The code within the foreach loop is exactly the same whether processed in reverse or normal order.
The above fails on the "foreach ("foreach")" line.
Obviously I could use if statements but then I would have to duplicate the do something code, which adds maintenance complexity.
Is there a solution to this?
Thanks for your help.