0

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?

0

4 Answers 4

2

It doesn't work 'cause for PHP, $_GET['myarray'] is a string not a variable, even it have a $ in it. You have to specify to use a variable which has this string for name.

So, you can use your array like this:

$arrayused = ${$_GET['myarray']};

foreach($arrayused as $item) {
    // ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

Also, if one would want to print array as a string, that probably usage of serialize would be a good way to do that, not ECHO.
Ah, ok, I assumed it would interpret as variable. That is the answer, although I couldn't get your ${$_GET['myarray']}; notation to work. The double '$$' Sergey suggested below did work for me though.
I should add also that I needed to modify the url to remove the '$' from the variable passed eg from 'myphppage.php?myarray=$array1' to myphppage.php?myarray=array1
1

If I understand you correctly - $arrayused is a string that can be 'array1' or 'array2'.

Than you should add additional $ to the beggining of variable (Variable Variables).

Here is the code sample:

<?php
$array1 = array(1,2,3,4);
$array2 = array(5,6,7,8);
?>

<?php
$arrayused = 'array1';
echo $arrayused;
?>

<?php foreach($$arrayused as $item): ?>
    some html code to show loop values
<?php endforeach; ?>

1 Comment

Sergey had more of the final answer giving me the '$$' that did work for me. Thanks.
1

Use a switch.

switch($_GET['myarray']) {
case '$array1':
    $arrayused = $array1;
    break;
case '$array2':
    $arrayused = $array2;
    break;
}

Comments

1

I would just send the key with the URL, then you can insert the key right away, and don't have to do fancy tricks. Try it like this:

// URL: http://www.domain.com/page.php?myarray=1
$my_arrays = array(
    1 => array(1, 2, 3, 4),
    2 => array(4, 5, 6, 7)
);

$array_used = $my_arrays[$_GET['my_array']];
foreach($array_used as $item) {
    // do stuff with the array
}

And if you want to be a bit more safe, or want to prevent errors, grab the array like this:

$array_used = array_key_exists($_GET['my_array'], $my_arrays) 
        ? $my_arrays[$_GET['my_array']] 
        : array();

The empty array (the 'else' part of the ternary) can be anything you like, but like this you don't have to do an extra check (like: if($array_used == 'NOTFOUND') {})

1 Comment

Thanks hadn't thought of array of arrays.

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.