user2426701
user2426701

Reputation:

PHP checkboxes group of another group of names

Please try to read carefully because it's not even easy to explain. I have a form that contains various inputs with the same names. This is why I am grouping them, like the example below:

<input class="" type="text" name="photoname[]"/>
<input class="" type="text" name="photodescription[]"/>
<input value="121001392" name="protagonist[][]" type="checkbox"/>
<input value="121001393" name="protagonist[][]" type="checkbox"/>
<input value="121001394" name="protagonist[][]" type="checkbox"/>
<input value="121001395" name="protagonist[][]" type="checkbox"/>

Now the groups work fine. The problem are the checkboxes, because those should be firstly grouped with the index of the other input fields and then having their own index. This is why for the beckboxes I tried to use double group like this [][] But the given array is wrong and the checkboxes are not grouped in the right way. This is the result:

Array
(

    [photoname] => Array
        (
            [0] => 8e98ee38864e74a9d5abf45edb263b8f
            [1] => 16fb2761e8cbe6eb877b2af8a95441dd
        )

    [protagonist] => Array
        (
            [0] => Array
                (
                    [0] => 121001392
                )

            [1] => Array
                (
                    [0] => 121001393
                )

            [2] => Array
                (
                    [0] => 121001394
                )

            [3] => Array
                (
                    [0] => 121001395
                )

        )

    [photodesc] => Array
        (
            [0] => example
            [1] => example
        )

)

But the expected result should be the following:

Array
(

    [photoname] => Array
        (
            [0] => 8e98ee38864e74a9d5abf45edb263b8f
            [1] => 16fb2761e8cbe6eb877b2af8a95441dd
        )

    [protagonist] => Array
        (
            [0] => Array
                (
                    [0] => 121001392
                    [1] => 121001393

                )

            [1] => Array
                (
                    [0] => 121001394
                    [1] => 121001395
                )

    [photodesc] => Array
        (
            [0] => example
            [1] => example
        )

)

In the expected result the first index is the index of all the other inputs in the form and the children index is the index of each selected checkbox. In this way I am able to loop the array and assign the checked checkboxes based on the parent index...How can achieve this?

Upvotes: 1

Views: 56

Answers (2)

Mike Brant
Mike Brant

Reputation: 71384

The behavior you are getting exactly matches what you should expect when using [][] as you are creating elements in a 2-D array on the fly. If you don't specify the first index, you will automatically create a new value for first index every time.

If you need to group these values, you will need to specify the first index like this

<input value="121001392" name="protagonist[0][]" type="checkbox"/>
<input value="121001393" name="protagonist[0][]" type="checkbox"/>
<input value="121001394" name="protagonist[1][]" type="checkbox"/>
<input value="121001395" name="protagonist[1][]" type="checkbox"/>

Upvotes: 1

CodeAngry
CodeAngry

Reputation: 12985

<input class="" type="text" name="photoname[]"/>
<input class="" type="text" name="photodescription[]"/>
<input value="121001392" name="protagonist[0][]" type="checkbox"/>
<input value="121001393" name="protagonist[0][]" type="checkbox"/>
<input value="121001394" name="protagonist[1][]" type="checkbox"/>
<input value="121001395" name="protagonist[1][]" type="checkbox"/>

You need to name your groups. [] means multiple values in same group. But [][] will not work. It must be [group1][] or [group2][].

Upvotes: 0

Related Questions