3

I have an array like:

$arr = array("a.b" => "a.b", "b.c" => "b.c");

When i print this it simply displays

Array ( [a.b] => a.b [b.c] => b.c )

But when i use a loop to use the key and value in checkbox name and values like:

<form action="" method="post">
    <ul>
    <?php
    foreach($arr as $k => $v){?>
        <li><input type="checkbox" name="<?php echo $v;?>" value="<?php echo $k;?>"><?php echo $v;?></li>
    <?php }?>
    </ul>
    <input name="sub" type="submit"/>
</form>

When i press the submit button the result shows like:

Array ( [a_b] => a.b [b_c] => b.c [sub] => Submit )

Look at the output array, the keys are changed, The dot(.) replaces with _.

Why this happen??

Tomanow
  • 7,247
  • 3
  • 24
  • 52
  • 3
    Possible duplicate of [Why strings in $\_POST can not contain a dot "."?](http://stackoverflow.com/questions/3742653/why-strings-in-post-can-not-contain-a-dot) – billyonecan Jun 06 '16 at 08:05
  • 1
    This probably duplicate of http://stackoverflow.com/questions/68651/get-php-to-stop-replacing-characters-in-get-or-post-arrays – Oli Jun 06 '16 at 08:12
  • You mean just for `register_global` the index changed the dot with underscore?? –  Jun 06 '16 at 08:12

1 Answers1

1

Please look at variables.external documentation

Dots and spaces in variable names are converted to underscores. For example <input name="a.b" /> becomes $_REQUEST["a_b"].

You can overcome this by using file_get_contents('php://input'); and then reading and parsing the input on your own if you cannot avoid dots in the variable names that come through GET or POST.

phreakv6
  • 2,135
  • 1
  • 9
  • 11