简体   繁体   English

在循环时覆盖php循环,然后中断?

[英]overwrite php loop while looping it, then break?

I was wondering if this code snippet is considered legal: 我想知道此代码段是否合法:

$arr = array(123,456,789,123,456,789);
foreach($arr as $a) {
    $arr = $a;
    break;
}
//prints 123
echo $arr;

It executes, but are there any pitfalls i should know using this method? 它执行,但是使用这种方法我应该知道有什么陷阱吗?


Update: Here is the actual problem 更新:这是实际的问题
You have an array as follows from the database query ( select * from table where code = $code ) 您从数据库查询中获得一个如下数组( select * from table where code = $code

 Array ( [0] => Array ( [id] => 1 [code] => 1234567 [member_id] => 7 ) [1] => Array ( [id] => 5 [code] => 1234567 [member_id] => ) [2] => Array ( [id] => 67 [code] => 1234567 [member_id] => 43 ) ) 

all you care about is finding the first (if any) row that has an empty member_id (this means the code has not been claimed). 您只需要查找具有空member_id的第一行(如果有)(这意味着未声明代码)。

So how do you go about doing this? 那么,您如何去做呢?

According to Felix Kling using the variable to hold the array of codes and then overwriting it with the row that you want is not the best solution, so what do you propose. 根据Felix Kling的说法,使用变量保存代码数组,然后用所需的行覆盖它不是最佳解决方案,因此您提出了什么建议。

Also, bonus credit: How many different 7 digit codes can you generate using 32 characters (duplicate characters allowed)? 另外,还有奖励积分:您可以使用32个字符(允许重复的字符)生成多少个不同的7位代码?

is it 32^32*7 or ((((((32^32)^32)^32)^32)^32)^32)^32)? 是32 ^ 32 * 7还是(((((((32 ^ 32)^ 32)^ 32)^ 32)^ 32)^ 32)^ 32)吗?

As others explained, there is no reason to reuse the variable. 正如其他人解释的那样,没有理由重用该变量。 Is there a reason for it? 有什么理由吗? Why not create a new one? 为什么不创建一个新的呢?

$resultId = 0;

foreach ($result as $row) {
   if ($row["member_id"] == "") {
      $resultId = $row["id"];
      break;
   }
}

But here is the big question - why don't you simply do that in SQL? 但这是一个大问题-为什么不简单地在SQL中这样做呢? You're already creating an SQL query. 您已经在创建SQL查询。 What's wrong with select * from table where code = $code and member_id = null ? select * from table where code = $code and member_id = null什么问题?

Btw, to answer your other question: Number of permutations with seven characters, each one with 32 different possibilities = 32^7. 顺便说一句,回答另一个问题:7个字符的排列数目,每个排列32种不同的可能性= 32 ^ 7。

So, 所以,

using this method works fine as long as you are only using a copy of the array. 只要仅使用数组的副本 ,使用此方法就可以正常工作。

If you were to pass in the array by reference foreach($code_info as &$code) then you will receive a Invalid argument supplied for foreach() error. 如果要通过引用foreach($code_info as &$code)传入数组,则将收到为foreach()错误提供Invalid参数

This might work, but it is definitely bad coding style and makes your code harder to understand. 这可能有效,但是绝对是不好的编码风格,使您的代码更难以理解。 Because if you use more meaningful variable names, it makes no sense anymore: 因为如果使用更有意义的变量名,它就不再有意义了:

$scores = array(123,456,789,123,456,789);
foreach($scores as $score) {
    $scores = $score;
    break;
}
//prints 123
echo $scores;

In this case, $scores suggests to hold something like a list, not a single element (or imagine you use $score_list ... even worse). 在这种情况下, $scores建议保留类似列表的内容,而不是单个元素(或者想象您使用$score_list ...甚至更糟)。 And it won't improve if you start giving your variables names that fit for different types of values ;) 如果您开始为变量提供适合不同类型值的名称,那么它就不会有所改善;)


Remember: Not everything that is possible should be done. 切记:并非所有可能的事情都应该做。 I would definitely avoid this. 我绝对会避免这种情况。

$empty_number=-1;
foreach($arr as $a=>$b)
{
  if($b['member_id']!='')
  {
    $empty_number=$a;
    break;
  }
}

The index for the empty number is stored in the $empty_number variable.. if there is no row with an empty member_id tag, the value of $empty_number is -1. 空编号的索引存储在$ empty_number变量中。.如果不存在带有空member_id标记的行,则$ empty_number的值为-1。

EDIT: 编辑:

For your bonus question, the maximum would be 32^7 = 34 359 738 368 对于您的奖金问题,最大为32 ^ 7 = 34359738368

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM