2010-08-28 8 views
2

Existe-t-il un moyen de transformer en la fonctionnalité PHP pour Submitting a multidimensional array via POST with php?Désactivation des tableaux multidimensionnels via POST en PHP

Alors que la soumission de <input type="text" name="variable[0][1]" value="..." /> produit un $_POST comme si ...

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

Pas comme si:

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

Je pense/espère une directive php.ini obscure ou quelque chose. ..?

Répondre

0

Il est un peu plus haut, mais si nécessaire, vous pouvez analyser manuellement le corps de la demande.

<?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; 
    } 
?> 
0

Je ne pense pas. Qu'est-ce que vous essayez de faire exactement?

Vous pouvez utiliser la variable (0) (1) ou variable_0_1 comme noms par exemple.

0

Ne croyez pas que vous pouvez faire cela. Je ne comprends pas non plus pourquoi tu devrais le faire. Mais cela devrait fonctionner:

$_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; 
} 
2

Non, mais rien ne vous empêche d'aller chercher la chaîne de requête (par $_SERVER['QUERY_STRING']) et l'analyse manuellement. Par exemple:

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