Make array from $_POST values

12,242

Solution 1

Let's name them menu_category_1, menu_category_2

You're using PHP, so let us name them menu_category[0], menu_category[1], etc (or menu_category[], menu_category[], etc) instead.

Then $_POST['menu_category'] or $_GET['menu_category'] will be an array.

which is going to be imploded and then update my database

Normalise your database. Express your one-to-many relationships using a foreign key and a second table, not by storing CSV data.

Solution 2

The best way, as everyone suggested above, is to pass everything as an array from the form itself. If you cannot modify the current code for any reason, this is how you process it in your current scenario.

    $menu_category = array();
    for ($i = 1; $i <= $_POST['key']; $i++) {
        if (!empty($_POST['menu_category_'.$i]))
           $menu_category[] = $_POST['menu_category_'.$i];

    }

Solution 3

Using [] syntax in the form is by far preferred.

However, if you don't want to do that for some reason, you can do this:

for ($x = 0; $x <= $_POST['key']; $x++) {
   $menu_category[] = $_POST["menu_category_$x"];
}
$menu_category[] = $_POST['menu_category_new'];

Solution 4

The best way to do it would be, when your posting the information, structure it like this.

<input type="text" name="cat_names[]" />
<input type="text" name="cat_names[]" />
<input type="text" name="cat_names[]" />

Now, when you post the data, the variable $_POST['cat_names'] will have 3 elements. If you have 4 input boxes, it will be a 4 element array.

Example:

<input type="text" name="cat_names[]" value="a" />
<input type="text" name="cat_names[]" value="b" />
<input type="text" name="cat_names[]" value="echo" />
<input type="text" name="cat_names[]" value="kitten" />

Post the data

$_POST['cat_names'] = array('a', 'b', 'echo', 'kitten');

Although there's no saying the order will be kept so, 'b' maybe before 'a'.

But it depends if your using inputs, or doing it through JavaScript. If your using JS you could send other information as well, or use JSON.

Solution 5

You name your form elements (all of the ones of the same type) as menu_category[] (and not menu_category_N).

When you submit the form, PHP will automatically arrange them into an array!

Share:
12,242
Claudio
Author by

Claudio

I started in this field less than a year ago just to make my own page and share with some friends. I came a long long way in this short period of time, I am proud of myself.

Updated on June 04, 2022

Comments

  • Claudio
    Claudio almost 2 years

    Let's start telling that I'm passing an x amount of variables via post from a form.
    Let's name them menu_category_1, menu_category_2, ..., menu_category_x, plus, maybe, menu_category_new (I'm using an if empty to check this last one variable).

    To make things easier I'm also sending the parameter $key (amount of variables starting from 0).
    Now I need to set them into a new variable $menu_category (array), which is going to be imploded and then update my database.

    How do I set up that new $menu_category variable to be an array containing all my variables named in the beginning?
    I was thinking of using a for loop but I can't come up with something useful. Thanks!!!


    I ended up going the Quentin way, just adding [] to the keys. I chose this just because it worked right away. However I'm going to dig deeper on janenz00's sugestion, which I personally think is the way to go. Thanks to all of you who took the time to go through this!!!!