Multidimensional arrays and optional_param_array

Multidimensional arrays and optional_param_array

by Conn Warwicker -
Number of replies: 1
Picture of Core developers Picture of Plugin developers
I am in the process of trying to clean-up my plugins to use moodle standards, and am removing direct references to $_POST and using optional_param_array, however I am running into problems with multidimensional arrays passed through to the script.

The data being passed through by the AJAX script is (for example):

    action: download_report

    params[report]: CP

    params[btn]: gt_run_report

    params[params][0][name]: categories

    params[params][0][value][]: 5

    params[params][1][name]: structureID

    params[params][1][value]: 5

    params[params][2][name]: extraAwardNames


If i print out the whole $_POST so you can see the data coming through on the script side:

Array
(
    [action] => download_report
    [params] => Array
        (
            [report] => CP
            [btn] => gt_run_report
            [params] => Array
                (
                    [0] => Array
                        (
                            [name] => categories
                            [value] => Array
                                (
                                    [0] => 5
                                )

                        )

                    [1] => Array
                        (
                            [name] => structureID
                            [value] => 5
                        )

                    [2] => Array
                        (
                            [name] => extraAwardNames
                        )

                )

        )

)

The optional param is being done as:

$params = optional_param_array('params', false, PARAM_TEXT);

$action = optional_param('action', false, PARAM_TEXT);


If I were to remove the params['params'] array from the data being passed through then this would work fine, as optional_param_array seems to loop through each element in the array and run clean_param() on it, assuming it's a string.
But with an array of arrays, like this, I get: 

    Coding error detected, it must be fixed by a programmer: clean_param() can not process arrays, please use clean_param_array() instead.


Is there any way to do this, using the optional_param* methods, and leaving my data intact? Or am I going to have to completely change how I am sending data through, to remove these multidimensional arrays?


Thanks.



Average of ratings: -
In reply to Conn Warwicker

Re: Multidimensional arrays and optional_param_array

by Conn Warwicker -
Picture of Core developers Picture of Plugin developers
I've got around this by basically writing my own version of optional_param_array which allows for multi-dimensional arrays.