0

I've sent arrays to CakePHP like this in the past:

$this->Form->input("Req.{$i}");

I've manually put {$i} in which is derived from the code itself in a loop. Ideally I wanted to put in something like Req.{} to generate the next key index, but it seemed only manually putting it in would work. Years later I need to have something like

$this->Form->input("Req.{$i}.list.{}");

And I'd like to avoid generating an {$i2}. Basically, I'm asking how to properly send multi-dimensional arrays to $this->request->data on POST, without having to specify an index name, much like we might have <input name='whatever[]'> in traditional PHP. I'm posting with jQuery AJAX, if that matters.

Update: Following drmonkeyninja's answer, I received

[list] => Array(
                            [0] => Array
                                (
                                    [name] => 
                                )

                            [1] => Array
                                (
                                    [value] => 
                                )

                            [2] => Array
                                (
                                    [req] => 
                                )
                 )

It seems it will be required that I make an $i2 as PHP/HTML have no way of knowing that I'm not trying to make a new array for each entry.

1 Answer 1

1

Normally in CakePHP you define the array path separating array indexes using .:-

$this->Form->input('Model.0.value'); // name="data[Model][0][value]"

If you don't want to specify a numerical index you can just use .. to represent an empty index:-

$this->Form->input('Model..value'); // name="data[Model][][value]"

Update

If you can't get the CakePHP array path syntax working in the way you need you can always manually set the input names in the input options:-

$this->Form->input('Model..value', ['name' => 'data[Model][][value]']);
Sign up to request clarification or add additional context in comments.

3 Comments

I thought so too, but I get this when I try with more than one sub-dimensional key, let's say I have name and value: [list] => Array ( [0] => Array ( [name] => 'x' ) [1] => Array ([value] => 'y' ). Maybe it's just impossible for CakePHP to determine that I don't want to create two NEW arrays, but rather that I want both name and value in the same array.
As an alternative you could manually set the input's name. See my updated answer.
Your reply was helpful in that it made me realize there's no solution for this without a $i2. I put the name as you have there, but I get [0] => ('value' => 'x'), [1] => ('name' => 'y') because PHP still thinks I'm making a new array for each [] it finds in the input name. I appreciate the help, I've updated my question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.