b3tac0d3
b3tac0d3

Reputation: 908

PHP POST/GET as associative arrays?

I'm have 4 forms that I want to share the same action. When the PHP starts, I'm trying to get the value of my array before I run the code. The array name should represent the column name that I want to update and the value will be the column value. I've tried this echo $_GET[0]; but it doesn't return the value I'm looking for.

My question is twofold:

  1. Is there a way to call the values as an associative?
  2. If there is(I'm sure I'm just doing something silly here), how do I identify the name of the variable?

Thanks!

Upvotes: 1

Views: 2157

Answers (2)

Kita
Kita

Reputation: 2634

To harness PHP's built-in support for automatic associative array generation directly from HTML FORM, here is a brief introduction:

http://php.net/manual/en/faq.html.php#faq.html.arrays

The following form will populate a multi-dimensional $_POST.

<form method="POST">
  <input name="words[]" value="...">
  <input name="words[]" value="...">

  <input name="foo[bar][]" value="...">
  <input name="foo[bar][]" value="...">
  <input name="foo[bar][]" value="...">

  <input name="values[0][0][]" value="...">
  <input name="values[0][0][]" value="...">
  <input name="values[0][0][]" value="...">
</form>

$_POST will be similar to

$_POST = [
  "words" => [
    0 => "...",
    1 => "..."
  ],
  "foo" => [
    "bar" => [
      0 => "...",
      1 => "...",
      2 => "..."
    ]
  ],
  "values" => [
    0 => [
      0 => [
        0 => "...",
        1 => "...",
        2 => "..."
      ]
    ]
  ]
];

Also, there is a related SO question that might be a help too:

How to get form input array into PHP array

Upvotes: 2

Arturs Smirnovs
Arturs Smirnovs

Reputation: 193

Try var_dump($_GET); You will see all variables.

Upvotes: 0

Related Questions