(solved) Re: [thelist] newbie: why <input name="something[]" ... /> ?

Zhang Weiwu zhangweiwu at realss.com
Wed Aug 24 22:03:20 CDT 2005


Jan Brasna wrote:

>> Is this the only/real reason to use empty []?
>
>
> Yes. It generates an array.
>
Okay, I got the reason. Seems this is too newbie question.

PHP (unlike ASP) ignores values with same name. E.g. if there are two
<input> under the same name, the last value is taken by PHP while the
first value is ignored. This happens to both POST and GET.

If it's necessary to crrect this, there are two functions provided by
php.net comment that could be useful

   function repairPost($data) {
       // combine rawpost and $_POST ($data) to rebuild broken arrays in $_POST
       $rawpost = "&".file_get_contents("php://input");
       while(list($key,$value)= each($data)) {
           $pos = preg_match_all("/&".$key."=([^&]*)/i",$rawpost, $regs, PREG_PATTERN_ORDER);       
           if((!is_array($value)) && ($pos > 1)) {
               $qform[$key] = array();
               for($i = 0; $i < $pos; $i++) {
                   $qform[$key][$i] = urldecode($regs[1][$i]);
               }
           } else {
               $qform[$key] = $value;
           }
       }
       return $qform;
   }

   // --- MAIN

   $_POST = repairPost($_POST);




More information about the thelist mailing list