0

i have this long form that requires multiple inputs, how can i properly insert these inputs into an array using a foreach loop?

assuming i have these:

$name = $_POST['name'];
$color = $_POST['color'];
$age= $_POST['age'];
$gender = $_POST['gender'];
$location = $_POST['location'];
...etc..

instead of typing such this..

$myarray = array('id'=>$id,'name'=>$name,'color'=>$color,'age'=>$age,'gender'=>$gender,'location'=>$location,etc...);

how can i let foreach loop insert the appropriate values into the array?

3
  • 7
    $_POST is already an array. Commented Nov 6, 2013 at 14:14
  • @vlcekmi3: $_POST is already an array and you are making it 2d. Commented Nov 6, 2013 at 14:14
  • Take a look at this answer: PDO Insert Array Using Key As Column Name Commented Nov 6, 2013 at 14:15

3 Answers 3

2
$myarray = $_POST;

This is enough. $_POST is already an array as Suresh Kamrushi stated.

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

Comments

0

all you need is this

$myarray = array();
foreach ($_POST as $key => $value){
   $myarray[$key] = $value;
}

1 Comment

I was writing exactly this answer. Sorry, can't vote due to reached daily voting limit.
0
$myarray = array();
foreach ($_POST as $post){
$myarray[] = $post;
}

Edit As correctly pointed out in the comments below, $myarray = array_values($_POST); works too.

3 Comments

The question is not very clear. But your foreach loop is useless. You can simply do $myarray = array_values($_POST); instead.
This is a useless foreach loop
@AmalMurali - Yep, that would do too. David, it's what the OP apparently asks for even though no abundance of info given in the question.

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.