Foreach 只取数组中的第一个值
我有以下代码:
$referrals = inputFilter($_POST['referralsIds']);
$array =explode(",",$referrals);
foreach($array as $key=>$value):
/
$s=mysql_query("SELECT * FROM users WHERE upline='".$userdata['id']."' AND id='$value'");
$num_rows=mysql_num_rows($s);
if($num_rows==0)
return 2;
// No error found and the update was succesful - Return success!
mysql_query("UPDATE users SET upline='' WHERE id='$value'");
mysql_query("UPDATE users SET rbalance=rbalance-".$sdata['direct_delete_fee'].", direct_referrals=direct_referrals-1 WHERE username='".$userdata['username']."'");
return 100;
endforeach;
$referrals 变量正在发布两个值 (10,11)。但是当我将其放入 foreach 循环中时,它只会使用第一个值 (10) 运行查询。如何才能遍历所有提交的值?
谢谢。
I have the following code:
$referrals = inputFilter($_POST['referralsIds']);
$array =explode(",",$referrals);
foreach($array as $key=>$value):
/
$s=mysql_query("SELECT * FROM users WHERE upline='".$userdata['id']."' AND id='$value'");
$num_rows=mysql_num_rows($s);
if($num_rows==0)
return 2;
// No error found and the update was succesful - Return success!
mysql_query("UPDATE users SET upline='' WHERE id='$value'");
mysql_query("UPDATE users SET rbalance=rbalance-".$sdata['direct_delete_fee'].", direct_referrals=direct_referrals-1 WHERE username='".$userdata['username']."'");
return 100;
endforeach;
The $referrals variable is posting two values (10,11).But when I put it in a foreach loop, it will only run the query with the first value (10). How to do so it run through ALL values submitted?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
for 循环中有一个
return 100
。它在第一次迭代时终止循环。return
语句终止封闭函数。如果您需要返回多个结果,请考虑将它们推送到数组中,然后在完成后返回该数组。You have a
return 100
inside your for loop. It's terminating the loop on its first iteration.return
statements terminate the enclosing function. If you need to return multiple results, consider pushing them onto an array and then returning the array once you're done.您的代码在每个您可能想要的结束之前返回
:
You have code returning before the end of your for each
You probably want:
所以你有两个值,然后你爆炸它? :O但我认为你的意思是$array。
return 100
将从函数以及 foreach 中返回。So you have two values, then you explode it ? :O But i think you meant $array.
You have
return 100
which will return from the function, and as well from foreach.无论您在循环的第一次迭代中使用什么函数,都会返回 100。返回将导致循环立即停止。
You are returning 100 from whatever function you're in on the first iteration of the loop. returning will cause the loop to stop immediately.