0

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.

4
  • You must use eval() to execute code stored in a string. Commented Feb 21, 2015 at 13:23
  • Thanks for your answer. I am aware that eval is dangerous but since I am providing the code from within my script it would seem OK to use. Commented Feb 21, 2015 at 13:29
  • eval() can still cause problems with error reporting, it isn't just a potential security issue..... and it's totally unnecessary to use in this situation Commented Feb 21, 2015 at 13:32
  • eval() is not always evil -> stackoverflow.com/a/951868/1407478 I can and will not judge if eval() is inappropriate here, all I say is that it is the way to go if you want to execute code stored in a string. Commented Feb 21, 2015 at 13:38

2 Answers 2

1

You can't simply create strings and expect them to be executed as code

switch ($data['entire_receipt']) {
    case 'Y' :
        $foreach = $cart;
        break;
    case 'N' :
    default  :
        $foreach = array_reverse($cart, true);
}

foreach ($foreach as $line => $item) {
   // do something
}

Note that $foreach is a copy of the $cart array, if you want to directly modify the $cart array in your foreach loop, then use the $line value as the key to $cart

Sign up to request clarification or add additional context in comments.

5 Comments

He can if he uses eval() ... But that is most of the time not very recommendable.
Yes, if you use eval() you can, but getting in the habit of using something as dangerous as eval is a bad habit to get into, especially when it's unnecessary to do so
eval() also has serious issues when it comes to error handling, and shouldn't be a recommended approach for any problem
I decided not to use eval().
Thanks Mark, I tried your solution and accepted it.
0

Yo should iterate the same variable and reverse the array if it's necessary.

if ($data['entire_receipt']) == 'N') {
    $cart = array_reverse($cart, true)
}

foreach ($cart as $line => $item) {
  // Do something
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.