-4

I need to go through $_POST, but I don't know which indexes are used (i want to be able to update multiple users values(inputs have user ID as name). When I go through $_POST with foreach I'm getting the values, but I don't know how to get user_id(index)

if(isset($_POST['credit_update'])){
foreach($_POST as $current){
}
}

i need to get index of $current

2
  • Welcome to Stackoverflow. Please follow the ask advice you needed to confirm before posting any question. Keep in mind that only you want something and you ask yourself how it is programmed does not qualify as a programming question per-se. What you ask for is a very basic and fundamental language feature and you should have at least read all manual pages of the keywords you use in your code before asking a question, like php.net/foreach. Commented Oct 27, 2012 at 10:34
  • Also the first entry in the related column is: PHP,how to get current index in a foreach loop for array? - look for existing questions before posting your own. Commented Oct 27, 2012 at 10:39

2 Answers 2

1
foreach($_POST as $index=>$current){
      //$index now contains the index
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can read all about foreach in the manual. Here is the specific syntax you are looking for:

foreach($_POST as $key => $current){
    //Do something
}

If you just want to update the array you can also get the element by reference in this way:

foreach($_POST as &$current){
    //If you update $current inside the loop
    //the element will be updated in the array
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.