$_POST to array

How can I add all the $_POST values to an array so I can access them via an index like $somearray[0] are the post values assigned an index or I have to call them by their name?

Umm…$_POST is already an array…
$_POST[‘form_input_name’]

Or use a foreach. But why the need to repurpose the already existing array?

Well I wanted to type numbers instead of the input name like $_POST[0]; but that did not work

So in other words you do not want to know where the data came from?
What stops $_POST[0] from being “email” when it should be “message”?
I’m not sure I follow the “logic” to this reasoning.

The following will accomplish what you are saying you would like to do:

$myPost = array_values($_POST);

But I agree with logic_earth. How will you know what values are which?

As logic earth said, $_POST is an array (so is $_GET). However, I have a hunch your looking for this…


<input name="form[name]">
<input name="form[street]">
<select name="choices[]" multiple>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
</select>

When submitted the $_POST array will contain two arrays with this structure…


array( 
  'form' => array 
    'name' => 'name entered'
    'street' => 'street entered'
  ),
  'choices' => array (
    0 => '1'
    3 => '4'
  )
)

Only the selected choices will be present in the array. Un-selected ones will be missing.

You’re assuming that the $_POST array will ONLY have the input values. It will also have the name/value pair of any hidden inputs and the submit button (assuming you’re using submit type input button).

So even if we set all the $_POST array keys to numbers you’d still get non-input values in the array.

I’m leaning towards the possibility you’re leaving out some criteria that is confusing everyone as to why you want to do this.

Unless you give us some more clarification or a working example there’s not much else anyone can do other than what the above posters have said.

Well I am not sure if what I am trying to acomplish is right, but I thought that each array element is given a value in order of appearance in the form like this

form
input1
input2
input3
form

and I would have

$_POST[0] is the same as $_POST[‘input1’]

What I thought is that on post input1 would be assigned index 0 input2 index 1 input3 index 2 and so on that way then the logic works because I will know what index 3 is, well at least based on that logic.

No, actualy I was looking for what tboronczyk said however, thank you.

There is never a guarantee that it will come to you in any order or that PHP will keep the order it receives it at. There is also nothing preventing third-parties between the user and your server from modifying the data, adding or replacing. Or users that actually modify the data. so on and so on.

There is a potential for a security hole here. So I would not do this.

Okay, I will just stick to adding the name of the value, I was just assuming that I could make it shorter, thak you all for your input.

You have to a name attribute on each form control that you want included in the post or get request. Omitting the attribute causes that input element to be omitted from the form. And since you have to name them, you might as well choose names that make sense. If you want the elements to be numerically iterable then you can use an array within the array as I demonstrated.