php - conditionally change array used in foreach loop -
i want conditionally change array looped though foreach loop , send var in
url = domain.com/myphppage.php?myarray=$array1
script has 2 arrays. $_get
used var url , foreach loop uses 1 of 2 arrays output array content on page.
<?php $array1 = array(1,2,3,4) $array2 = array(5,6,7,8) ?> <?php $arrayused = $_get['myarray']; echo $arrayused; ?> <?php foreach($arrayused $item): ?> html code show loop values <?php endforeach; ?>
the var passed url shown on page output echo $arrayused
.
i expecting $arrayused
variable picked foreach
, looped through whichever of 2 arrays in variable.
however, doesn't pick variable , not go foreach
loop. if hardcode either of 2 array names foreach
statement, foreach
loop works fine.
why doesn't foreach statement appear 'recognize' variable retrieved url? prints on page before loop available?
if understand correctly - $arrayused string can 'array1' or 'array2'.
than should add additional $ beggining of variable (variable variables).
here code sample:
<?php $array1 = array(1,2,3,4); $array2 = array(5,6,7,8); ?> <?php $arrayused = 'array1'; echo $arrayused; ?> <?php foreach($$arrayused $item): ?> html code show loop values <?php endforeach; ?>
Comments
Post a Comment