I want to conditionally change the array that is looped though with foreach loop and send var in
Url = domain.com/myphppage.php?myarray=$array1
Script has two arrays. Also $_GET is used to get var from url and finally foreach loop that uses one of the two arrays to output array content on to the page.
<?php
$array1 = array(1,2,3,4)
$array2 = array(5,6,7,8)
?>
<?php
$arrayused = $_GET['myarray'];
echo $arrayused;
?>
<?php foreach($arrayused as $item): ?>
some html code to show loop values
<?php endforeach; ?>
The var is passed by url as it is shown on page as output by echo $arrayused.
I am expecting that the $arrayused variable will also be picked up by the foreach and looped through whichever of the two arrays that is in the variable.
However, it doesn't pick up variable and does not go into the foreach loop. If I hardcode either of the two array names into the foreach statement, then the foreach loop works fine.
Why doesn't the foreach statement appear to 'recognize' the variable that is retrieved from url? It prints on page just before the loop so it is obviously available?