1

I have this array $newArray, it has been built inside of a POST form, so I want to send the whole array in a INPUT as hidden:

Array
(
    [0] => Array
        (
            [day1to7] => 1
            [timeHHMM] => 10:00
        )

    [1] => Array
        (
            [day1to7] => 1
            [timeHHMM] => 11:00
        )

    [2] => Array
        (
            [day1to7] => 1
            [timeHHMM] => 12:00
        )

    [3] => Array
        (
            [day1to7] => 5
            [timeHHMM] => 14:00
        )

)

Could you please help me to know how shall I write it in INPUT?

echo '<input type="hidden" name="newArraySend" value="'. $newArray[day1to7]['timeHHMM'] . '">';  -> this is my wrong try

And also, please let me know how could I receive it?

$newArrayReceived = $_POST['newArraySend']; ->this also is wrong I think

Thank you very much in advance, Felipe

4
  • what you want to pass with your hidden input? the whole array in your case 4 element or just one element with day1to7 and timeHHMM Commented Jun 28, 2018 at 15:49
  • HI Being Sunny, I want to pass the whole array. Commented Jun 28, 2018 at 15:51
  • There is many ways but the easier and the most secure would be to store it in $_SESSION if you want to avoid it to be edited and having to re-validate the data Commented Jun 28, 2018 at 15:52
  • Possible duplicate of Passing Array Using Html Form Hidden Element Commented Jun 28, 2018 at 15:58

1 Answer 1

2

To answer the specific question; you could loop and add the hidden inputs:

foreach($newArray as $key => $val) {
    echo '<input type="hidden" name="newArraySend['.$key.'][day1to7]" value="'.$val['day1to7'].'">';
    echo '<input type="hidden" name="newArraySend['.$key.'][timeHHMM]" value="'.$val['timeHHMM'].'">';
}

Then the receiving PHP should use $_POST['newArraySend'] just as you would the original array.

Or just encode the entire array:

$val = htmlentities(json_encode($newArray));
echo '<input type="hidden" name="newArraySend" value="'.$val.'">';

Then decode on the receiving end:

$result = json_decode(html_entity_decode($_POST['newArraySend']), true);

But really this might be better handled with a session:

session_start();
$_SESSION['newArray'] = $newArray;

Then on the receiving end:

session_start();
$result = $_SESSION['newArray'];
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you AbraCadaver, second option for me is the best ! :)
NP, added an alternative.
Interesting, thank you so much again :) By the way, just there is one "dot" missing in "Or just encode the entire array" concatenating $val. :) Thanks again

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.