2012-10-27 61 views
-4

我需要通过$_POST,但我不知道使用了哪些索引(我希望能够更新多个用户值(输入具有用户ID作为名称)当我通过foreach时通过$_POST我得到价值观,但我不知道怎么去user_id(index)php - 从foreach获取项目的索引?

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

我需要得到的$current

+0

欢迎#1。请在发布*任何*问题之前,按照您需要确认的[问题建议](http://stackoverflow.com/questions/ask-advice)进行确认。请记住,只有你想要的东西,你问自己如何编程不符合本身的编程问题。你所要求的是一个非常基本和基本的语言功能,你应该至少在提问之前阅读你在代码中使用的所有关键字的手册页,例如http://php.net/foreach。 – hakre

+0

此外,相关列中的第一个条目是:[PHP,如何在数组的foreach循环中获取当前索引?](http://stackoverflow.com/q/1450157/367456) - 在发布之前查找现有问题拥有。 – hakre

回答

0

你可以阅读所有关于the manual的foreach指数。下面是具体的语法你寻找:

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

如果你只是想更新数组,你也可以通过参考以这种方式获得的元素:

foreach($_POST as &$current){ 
    //If you update $current inside the loop 
    //the element will be updated in the array 
} 
1
foreach($_POST as $index=>$current){ 
     //$index now contains the index 
}