3

I am having an strange issue while transversing array, created by html input. Here is my html:

<input type="hidden" name="new_date['rule'][]" value="hfgfgfgfg">
<input type="date" name="new_date['date'][]" />

if I submit form then I get array like this:

$var=$_POST['new_date'];
echo"<pre>";
print_r($var);
echo"</pre>";
//output
Array
(
    ['rule'] => Array
        (
            [0] => rule_5a6c50ff02fff
        )

    ['date'] => Array
        (
            [0] => 2018-05-24
        )

)

if I am trying the array with key rule then I am getting nothing or empty.

print_r($var['rule']);
//output
.....empty......

Please help. TIA

3
  • 2
    try with $var["'rule'"] Commented May 18, 2018 at 7:37
  • yes, its working @ChetanAmeta... Thank you... please make it answer I will accept it as answer :) Commented May 18, 2018 at 7:45
  • thanks :), I put my answer Commented May 18, 2018 at 7:51

4 Answers 4

4

In HTML you have used new_date['rule'][] which have key as 'rule' so while retriving through POST you need to use same key i.e. with single quote

USE below:

var_dump($var["'rule'"])

To use : $var['rule'] you may need to use new_date[rule][] in your html

Sign up to request clarification or add additional context in comments.

Comments

4

You can try this

<input type="hidden" name="new_date[rule][]" value="hfgfgfgfg">
<input type="date" name="new_date[date][]" />

Comments

2

This will work

echo $var["'rule'"][0];

This is because you are using

name="new_date['rule'][]"

in HTML. Instead you should use

name="new_date[rule][]"

Hopefully, this will solve your problem

If you var_dump() $var, this will be the output:

array(2) {
  ["'rule'"]=>
  array(1) {
    [0]=>
    string(9) "hfgfgfgfg"
  }
  ["'date'"]=>
  array(1) {
    [0]=>
    string(10) "2018-05-11"
  }
}

Comments

1

you can try using $var["'rule'"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.