Can I use Get or Post to read an Array?

Asked

Viewed 541 times

1

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

This 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 don’t know how to put the array inside GET, I searched some forums and tested the ways they were made, but I was unsuccessful

  • 2

    Yes, it is possible! echo $_GET['idArray'][0]; you will need a for to list all values.

  • 1

    Just do $ids = $_GET['idArray'];. Then just a for or foreach to show/run all.

2 answers

2

The simplest way is to take it directly by the name of array:

<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";
}

0

On 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 more the question and use POST.

  • 2

    I get your idea, but I think if you put the "size" out of the "for" is better. The way it is, there will be an "size" entry for each item.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.