0

I'm trying to store the users's input via the method get in an array to store it and further process it without overwriting the initial get-value. But I dont know how.. do I have to store them in a database to do that? Or can I just push every input into an array?

3
  • Copy them to a variable... this is basic PHP so you may want to check a tutorial for the basics. Commented Apr 9, 2018 at 18:30
  • Please provide some sample code to make it easier for folks to understand your question. Commented Apr 9, 2018 at 18:31
  • MrLeeh, this question is extremely simple. He essentially is taking the $_GET array from PHP, and he wants to be able to store it in another array so he can manipulate it w/o affecting the original $_GET array. He can do: $newArray = $_GET or even $newArr = $_POST depending on how he wants to set up his form controller. The other answer, which only adds "accepted GETs" to the new array can be found below :) Commented Apr 9, 2018 at 18:38

3 Answers 3

2

I believe the following should work for you... This will take all the $_GETs that you supply and put them in a new array so you can modify them without affecting the original $_GET array.

if(is_array($_GET)){
 $newArr = $_GET; // modify $newArr['postFieldName'] instead of $_GET['postFieldName'] to preserve original $_GET but have new array. 
}

That solution there will dupe the $_GET array. $_GET is just an internal PHP array of data, as is $_POST. You could also loop through the GETs if you do not need ALL of the GETs in your new array... You would do this by setting up an accepted array of GETs so you only pull the ones you need (this should be done anyways, as randomly accepting GETs from a form can lead to some trouble if you are also using the GETs for database/sql functions or anything permission based).

if(is_array($_GET) && count($_GET) > 0){
     $array = array(); 
     $theseOnly = array("postName", "postName2"); 
     foreach($_GET as $key => $value){
      if(!isset($array[$key]) && in_array($key, $theseOnly)){ // only add to new array if they are in our $theseOnly array. 
       $array[$key] = $value; 
      } 
     }
     print_r($array); 
    } else {
     echo "No $_GET found."; 
    }
Sign up to request clarification or add additional context in comments.

4 Comments

I actually had something very similar to your first suggestion, but that overwrites the first array value in the $newArr. So Array ( [0] => Array ( [enteredValues] => Test123) ) is always the last input from the user. And I'm not advanced enough to understand your second solution but that does'nt work for me either..
Ah, sois 'enteredValues' the name that is submitted multiple times? If it is, insetad of doing $newArr = $_GET, do $newArr = $_GET['enteredValues']; and that SHOULD allow you to have a new array of all the entered values :) At this point though I am just guessing. Can you please add some code for where data is being SENT and then post the code for it being RECEIVED. If you do that, I should be able to see what the array strucutre is - or better yet, just do: print_r($_GET) and let me know what that says. I can then give you the answer :) be back soon, going to store.
Yeah if enteredValues is an array do $newArr = $_GET["enteredValues"] or if you use my 2nd suggestion just do foreach ($_GET["enteredValues"] as $key => $value). You will get an array like: array(0=>value1, 1=>value2); this way
So basically I have this form for testing:<form method="get"> <input type="text" name="enteredValues"> <input type="submit" value="Go"> </form> and if I use your first suggestion: if(is_array($_GET)){ $newArr = $_GET["enteredValues"]; } print_r($newArr); it still only gives me the last entered Value.. :(
1

I would just add to what @Nerdi.org said.

Specifically the second part, instead of looping through the array you can use either array_intersect_key or array_diff_key

$theseOnly = array("postName", "postName2");
$get = array_intersect_key( $_GET, array_flip($theseOnly);

//Or

 $get = array_diff_key( $_GET, array_flip($theseOnly);

array_intersect_key

array_intersect_key() returns an array containing all the entries of array1 which have keys that are present in all the arguments.

So this one returns only elements you put in $theseOnly

array_diff_key

Compares the keys from array1 against the keys from array2 and returns the difference. This function is like array_diff() except the comparison is done on the keys instead of the values.

So this one returns the opposite or only elements you don't put in $theseOnly

And

array_flip

array_flip() returns an array in flip order, i.e. keys from array become values and values from array become keys.

This just takes the array of names with no keys (it has numeric keys by default), and swaps the key and the value, so

 $theseOnly = array_flip(array("postName", "postName2"));
 //becomes
 $theseOnly = array("postName"=>0, "postName2"=>1);

We need the keys this way so they match what's in the $_GET array. We could always write the array that way, but if your lazy like me then you can just flip it.

1 Comment

Yeah, only reason I have loop is because he intends to modify the copy of the original array. With the loop he can modify them as needed before they go into new array without changing original. But yeah, without individual modification (if he just wants 2 arrays for a function or something else to modify later) then I suppose your methods are more efficient.
0
session_start();
if(!isset($_SESSION['TestArray'])) $_SESSION['TestArray'] = array();
if(is_array($_GET)){
    $_SESSION['TestArray'][] = $_GET;
}
print_r($_SESSION['TestArray']);

Thanks everybody for helping! This worked for me!

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.