How to pass an array?

How to pass an array using PHP using the GET method?

thanks

+3
source share
4 answers

In your query string (or POST data it does not really matter), you should end up with:

http://example.com/myscript.php?foo[]=1&foo[]=2&foo[]=3

PHP will parse this on $_GET["foo"], and it will be an array with members 1, 2, and 3. How you control which of the forms is up to you. In the past, I have called various checkboxes “check []”, for example.

+10
source

Do you mean how through the form?

<form method="GET" action="action.php">
    <input type="text" name="name[]">
    <input type="text" name="name[]">
    <input type="submit" value="Submit">
</form>

Note that there is a name attribute [].

Then in your php:

<?php
    $names = $_GET['name'];
    foreach($names as $name) {
        echo $name . "<br>";
    }
?>
0
source

script :

http://www.example.com/script.php?foo[bar]=xyzzy&foo[baz]=moo

$_GET:

array(1) { ["foo"]=> array(2) { ["bar"]=> string(5) "xyzzy" ["baz"]=> string(3) "moo" } }

, .

0

As an aside, instead of passing a GET array, it makes sense to store the array in $ _SESSION and get it again later.

-1
source

Source: https://habr.com/ru/post/1722348/


All Articles