views:

32

answers:

4

Is there a way to turn off the PHP functionality for Submitting a multidimensional array via POST with php?

So that submission of <input type="text" name="variable[0][1]" value="..." /> produces a $_POST like so...

array (
    ["variable[0][1]"] => "...",
)

NOT like so:

array (
    ["variable"] => array(
                          [0] => array (
                                        [1] => "..."
                         ),
    ),
)

I'm thinking/hoping an obscure PHP.ini directive or something... ?

A: 

I should think not. What exactly are you trying to do?

You could use variable(0)(1) or variable_0_1 as names for example.

Zahymaka
A: 

Don't believe you can do that. I also don't understand why you'd need to. But this should work:

$_POST['variable'] = array(array('abc','def'),array('ddd','ggg'));

print_r(flatPost('variable'));

function flatPost($var)
{
    return enforceString($_POST[$var], $var);
}

function enforceString($data, $preKey = '')
{
    if(!is_array($data))
    {
        return array($preKey . $data);
    }

    $newData = array();
    foreach($data as $key => &$value)
    {
        $element = enforceString($value, $preKey . '[' . $key . ']');
        $newData = array_merge($newData, $element);
    }
    return $newData;
}
Tim
+2  A: 

No, but nothing stops you from fetching the query string (through $_SERVER['QUERY_STRING']) and parsing it manually. For instance:

$myGET = array();
foreach (explode("&", $_SERVER['QUERY_STRING']) as $v) {
    if (preg_match('/^([^=])+(?:=(.*))?$/', $v, $matches)) {
        $myGET[urldecode($matches[1])] = urldecode($matches[2]);
    }
}
Artefacto
A: 

It's a little over the top, but if necessary you could manually parse the request body.

<?php
    if(!empty($_POST) && $_SERVER['CONTENT_TYPE'] == 'application/x-www-form-urlencoded') {
            $_post = array();
            $queryString = file_get_contents('php://input'); // read the request body
            $queryString = explode('&', $queryString); // since the request body is a query string, split it on '&'
                                                       // and you have key-value pairs, delimited by '='
            foreach($queryString as $param) {
                    $params = explode('=', $param);
                    if(array_key_exists(0, $params)) {
                            $params[0] = urldecode($params[0]);
                    }
                    if(array_key_exists(1, $params)) {
                            $params[1] = urldecode($params[1]);
                    }
                    else {
                            $params[1] = urldecode('');
                    }
                    $_post[$params[0]] = $params[1];
            }
            $_POST = $_post;
    }
?>
LeguRi