Can I use Get or Post to read an Array?

1

I'm trying to make an .php file read an array, but I also want to assign an id for each value in Vector.

Ess is the html

<form method='GET'>
 <input type="hidden" name="idArray[]" value="1" />
 <input type="hidden" name="idArray[]" value="2" />
</form>

in php

$id1 = $GET[''];

This is where I get lost because I do not know how to put the array inside the GET, I looked in some forums and tried the ways they were done, but I did not succeed.

    
asked by anonymous 09.12.2015 / 17:27

2 answers

2

The simplest way is to get directly by the array name:

<form method='GET'>
 <input type="hidden" name="idArray[]" value="1" />
 <input type="hidden" name="idArray[]" value="2" />
</form>

And in PHP

$ids = $_GET['idArray'];

Then just use the values as you see fit. For example:

foreach ( $ids as $id ) {
   echo $id . "<br>\n";
}

Or even as @rray commented:

$count = count( $ids );   // fora do for, senão o PHP reprocessa a cada iteração.
for($i = 0; $i < $count; ++$i) {
   echo $ids[$i] . "<br>\n";
}
    
09.12.2015 / 22:53
0

In the form:

<form method='GET'>
   <?php for($i=1;i<count($array)+1;i++){?>
   <input type="hidden" name="tamanho" value="<?=count($array)?>" />
   <input type="hidden" name="array<?=$i?>" value="<?=$i?>" />
   <?php } ?>
</form>

No GET:

$tamanho = $_GET['tamanho'];

for($i=1;i<$tamanho+1;i++){
   echo $_GET['array'.$i];
}

That would be the idea, specify the question further and use POST.

    
09.12.2015 / 20:00