0

I have a general question. I wrote the below code to add an array of arrays to the $_POST variable. I know the post variable is an array too. So, after adding my multidimensional array to the post variable one by one, I tried to print_r the data to view it but, only one array would print out.

why is it that only one array would print out?

$x = 0;
    for($x; $x < count($return_auth); $x++){
        $_POST = $return_auth[$x];
    }

 print_r($_POST);
2
  • The obvious question from us is: what is $return_auth? And please include a dump if it. Commented Sep 29, 2017 at 17:19
  • Do not modify PHP superglobals. Ever. Take the data you want out and put it somewhere else. Commented Sep 29, 2017 at 17:31

3 Answers 3

3

Although nerdlyist answer is absolutely right, but I think it is not the right way, in short, you are modifying $_POST variable and since $_POST variable has a meaning attached to it that it contains all the parameters sent in that POST request. If you overwrite it that change will be affected throughout your application and other modules running in your application will see an additional post parameter which is not actually sent in that request in $_POST array which is not right IMO.

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

2 Comments

Good point that should not be taken lightly. My gut feeling was to not mess with POST and GET but I wasn't sure about it. Better to copy POST and mess with the copy.
No other Modules in the Application will see this unless the data is then sent (POST or GET) to that portion of the application. A $_POST can be modified and then resent as form data say in an api.
2

What you are doing is overwriting post. Based on what you have here you could do something like this:

$_POST[return_auth] = $return_auth;

Unless you have a reason to loop an array to create an array...

Comments

1

use this in your for loop

$_POST[] = $return_auth[$x];

Edit

this will work better

$_POST['something' . $x] = $return_auth[$x];

now you can access to (foo) for example

$_POST['somethingfoo'];

2 Comments

Good catch. Didn't see that
Something does not seem right about a numeric index right on the $_POST global. Cannot find anything stating something negative but I would be careful with this.

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.