0

Suppose I have 2 arrays $test24 and $test48 . Now if I receive a web form POST $type value 24 I need to execute this

foreach ($test24 as $val) 
{
code here
}

while if I receive a web form POST $type value 48 I need to execute this

foreach ($test48 as $val) 
{
code here
}

I am trying to aggregate/merge the code above in a single foreach in this way

foreach ($test$type as $val) 
{
 code here
}

or this

foreach ($test.$type as $val) 
{
code here
}

but none of these two solutions works , do you know a solution ?

1 Answer 1

1

Format of variable for your case is:

foreach (${'test' . $type} as $val) 

But I advise you to use array instead of two variables:

$test = [24 => [1,2,3], 48 => [4,5,6]];


foreach ($test[$type] as $val) {
    echo $val;
}
Sign up to request clarification or add additional context in comments.

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.