[Solved] avoid php post variables rewriting [closed]


Your question is a little unclear, but if you’re trying to pass an array using a single form input the short answer is no, using a single element you cannot pass an array into the POST array (with the exception of the multi-select form element), but it’s easy with a tiny bit of processing once you submit. You just use a delimiter on the value and explode it in PHP:

In HTML:

<input name="value" value="1|2|4|4|5" />

In PHP

$values = explode('|',$_POST['value']);

This will result in:

$values[0] == 1;
$values[1] == 2;
...

However, there is never a way to get a PHP array to have multiple values for a single key at the same time, so you can never have a PHP array that looks like:

$_POST['value'] = 1;
$_POST['value'] = 2;
$_POST['value'] = 3;
$_POST['value'] = etc;

Because for any array (_POST or otherwise) $array[KEY] can not have two values (i.e. how can if ($_POST['value'] === $_POST['value']) ever not be true? It can’t, any more that if ($x===$x) or if (1===1) can be false). You can, however, use a multi-dimensional array, which would look like:

$_POST['value'][0] = 1;
$_POST['value'][1] = 2;
$_POST['value'][2] = 3;
$_POST['value'][3] = 'etc';

and then work with it by:

foreach($_POST['value'] as $key =>$value){
    echo $value.',';
}

which would output

1,2,3,etc

2

solved avoid php post variables rewriting [closed]