Foreach 只取数组中的第一个值

发布于 2024-12-01 21:49:54 字数 890 浏览 1 评论 0原文

我有以下代码:

         $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ぽ尐不点ル 2024-12-08 21:49:54

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.

紧拥背影 2024-12-08 21:49:54

您的代码在每个您可能想要的结束之前返回

     return 100; //HERE
 endforeach;

endforeach;
return 100;

You have code returning before the end of your for each

     return 100; //HERE
 endforeach;

You probably want:

endforeach;
return 100;
新一帅帅 2024-12-08 21:49:54

所以你有两个值,然后你爆炸它? :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.

转角预定愛 2024-12-08 21:49:54

无论您在循环的第一次迭代中使用什么函数,都会返回 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文