0

I have a form which is posting JSON encoded data to a Controller function, the reason for this is that the form is dynamic and may contain so many elements that it is limited by Php configuration.

I have no problem retrieving the JSON data in my controller, and I can decode it into an array using:

$result = json_decode($this->request->data['Address']['result'], true);

My issue, however, is that the resultant data is in the following format:

array(
    'data[Address][id][0][2087]' => '2087',
    'data[Address][id][0][2680]' => '2680',
    'data[Address][id][1][3168]' => '3168',
    'data[Address][id][1][3911]' => '3911',
    'data[Address][id][2][1818]' => '1818',
);

However, I would like to convert it to the CakePHP array style, ie:

array(
      'data' => array(
                    'Address' => array(
                                 'id' => array(
                                              0 => array(
                                                      2087 => '2087',
                                                      2680 => '2680
                                                        ),
                                              1 => array(
                                                      3168 => '3168',
                                                      3911 => '3911'
                                                        )
                                               )
                                    )
                          )
           )

I realise this could be done through iteration over the array, but since CakePHP does this already internally, is there some way to simply access that core method and have it convert the array into a multidimensional array?

user984976
  • 1,314
  • 1
  • 12
  • 21

2 Answers2

0

Assuming you have a set number of fields for each array, you can you use PHP's array_chunk().

Otherwise, I think your best bet (unfortunately) is to iterate like you mentioned.

Dave
  • 28,833
  • 23
  • 113
  • 183
0

I agree with @thaJeztah : forms should be using standard HTTP 'POST' to post forms. Even jQuery can POST a form content. Converting the form data to json is not standard. If you let the form POST normally, you can get the data from $_POST array.

In any case, you can use PHP's URL-parsing functions:

$urlstring = http_build_query($result);
parse_str($urlstring , $result_array);

$result_array will be a multi-dimensional array.

Costa
  • 4,851
  • 33
  • 30
  • My issue with converting to a serialized / stringified object (I am using JSON.stringify to convert the whole form into a single string), is that the form may contain up to 1000 elements (the page contains a large number of checkboxes), and it seems that once I go over a certain number of elements php / cakephp cuts off the remainder of the form data ? It simply seems to have a limit on the number of elements. Can you suggest another way to post a large number of elements without hitting this limitation? – user984976 May 28 '13 at 22:29
  • Don't see why it would be cutting it off. Have you check PHP's [POST_MAX_SIZE](http://php.net/ini.core.php#ini.post-max-size) setting? Your current method isn't "wrong", it's just seems like an unnecessary extra step. :) – Costa May 29 '13 at 01:51
  • @user984976 the reason the array is probably cut-off is because the Suhosin patch is installed and configured to limit the size of arrays (see this question with some suggestions: [$_POST max array size](http://stackoverflow.com/questions/9807100/post-max-array-size) and this website: [Suhosin](http://www.hardened-php.net/suhosin/)). You can either change those settings or wonder if all inputs need to be sent at all times. Since you're allready using AJAX, would it be an option to update fields/values individually? I.e. insert/update individual address records when changed? – thaJeztah May 29 '13 at 06:51