PHP - 使用for循环计算数组中的元素,而不是使用内置的PHP函数计数

时间:2021-09-04 14:09:17

I'm working on getting a better understand on for loops and how I can use a for loop to count elements in an array without the built-in PHP function count.

我正在努力更好地理解for循环以及如何在没有内置PHP函数计数的情况下使用for循环来计算数组中的元素。

I've been able to do it with the foreach loop, (count the elements in an array) but for some reason the for loop isn't working?

我已经能够使用foreach循环,(计算数组中的元素),但由于某种原因,for循环不起作用?

Foreach loop this works:

Foreach循环有效:

$month_temp = "78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 81, 76, 73, 68, 72, 73, 75, 65, 74, 63, 67, 65, 64, 68, 73, 75, 79, 73";

$temp = explode(',', $month_temp);

$count = 0;

foreach ($temp as $value) {
    $count++;
}
print_r($count);

Can someone point out what I'm doing wrong? (I end up with some kind of endless loop without anything being displayed)

有人可以指出我做错了吗? (我最终得到了一些没有任何东西显示的无限循环)

For loop not working:

对于循环不工作:

$month_temp = "78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 81, 76, 73, 68, 72, 73, 75, 65, 74, 63, 67, 65, 64, 68, 73, 75, 79, 73";

$temp = explode(',', $month_temp);

$count = 0;

for ($i = 0; $i < $temp; $i++) {
    $count++;
}
print_r($count);

4 个解决方案

#1


People are saying you need to use count() to limit your loop. But if you were to use count() then whats the purpose? cause your question says you want to see the count without the use of count().

人们说你需要使用count()来限制你的循环。但是如果你使用count()那么目的是什么呢?因为你的问题说你想在不使用count()的情况下看到计数。

You have to raise the limit considerably long and let the loop go on until there are no more elements in your array then you can break the loop

你必须长时间提高限制并让循环继续,直到你的数组中没有更多的元素然后你可以打破循环

$count = 0;
for ($i = 0; $i < 1000000; $i++) {
     if(isset($temp[$i]))   
        $count++;
     else
         break;
}
print_r($count);

Another simplified version

另一个简化版本

$count = 0;
for ($i = 0; TRUE; $i++) {
     if(isset($temp[$i]))  
        $count++;
     else
         break;
}
print_r($count);

Fiddle

Fiddle 2

#2


Right now, you're using the exploded string (the array) inside your for:

现在,你正在使用你内部的爆炸字符串(数组):

$temp = explode(',', $month_temp);
^
|------------------v
for ($i = 0; $i < $temp; $i++) {
// doesn't make sense

Sidenote: Just use the count() function if your intent is to get the number of elements inside the exploded array. That loop is actually superfluous.

旁注:如果您的目的是获取爆炸数组中的元素数量,只需使用count()函数。那个循环实际上是多余的。

Note: You can device a way using for without using a count thru array pointers:

注意:您可以使用for设备,而无需使用通过数组指针计数:

$month_temp = "78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 81, 76, 73, 68, 72, 73, 75, 65, 74, 63, 67, 65, 64, 68, 73, 75, 79, 73";
$temp = explode(',', $month_temp);
for (current($temp); ($i = current($temp)) !== false; ($i = next($temp))) {
    // do something
    echo trim($i) , '<br/>';

}

Sample Output

#3


$i < $temp is not the condition you want. I'm not certain how PHP is doing the comparison, but you're comparing an integer to an array. That's always evaluating as true, so the loop continues for eternity.

$ i <$ temp不是你想要的条件。我不确定PHP如何进行比较,但是你将整数与数组进行比较。这总是评估为真,所以循环持续永恒。

Ironically, you want $i < count($temp)! That will let the loop run until $i is the same as the number of elements in the array, at which point you'll have three expressions ($i, $count and count($temp)) representing the number of elements in the array.

具有讽刺意味的是,你想要$ i ($>

So this is perhaps not the best way to test your for loop skills. Perhaps try running through a collection of integers to find the index of a particular number, to find the sum of all the numbers, the largest number, the smallest number, the range, the average, etc etc :)

所以这可能不是测试你的for循环技能的最佳方法。也许尝试运行整数集合来查找特定数字的索引,找到所有数字的总和,最大数字,最小数字,范围,平均值等等:)

#4


you can use sizeOf function in place of count like

您可以使用sizeOf函数代替计数

$max = sizeof($huge_array);
for($i = 0; $i < $max;$i++)
{
code...
}

If you do not want to use count or sizeof then it better you use

如果你不想使用count或sizeof那么你最好使用它

foreach($arr as $ar){
//Code here
}

Except this i do not thing there is any method available.

除此之外我没有任何方法可用。

#1


People are saying you need to use count() to limit your loop. But if you were to use count() then whats the purpose? cause your question says you want to see the count without the use of count().

人们说你需要使用count()来限制你的循环。但是如果你使用count()那么目的是什么呢?因为你的问题说你想在不使用count()的情况下看到计数。

You have to raise the limit considerably long and let the loop go on until there are no more elements in your array then you can break the loop

你必须长时间提高限制并让循环继续,直到你的数组中没有更多的元素然后你可以打破循环

$count = 0;
for ($i = 0; $i < 1000000; $i++) {
     if(isset($temp[$i]))   
        $count++;
     else
         break;
}
print_r($count);

Another simplified version

另一个简化版本

$count = 0;
for ($i = 0; TRUE; $i++) {
     if(isset($temp[$i]))  
        $count++;
     else
         break;
}
print_r($count);

Fiddle

Fiddle 2

#2


Right now, you're using the exploded string (the array) inside your for:

现在,你正在使用你内部的爆炸字符串(数组):

$temp = explode(',', $month_temp);
^
|------------------v
for ($i = 0; $i < $temp; $i++) {
// doesn't make sense

Sidenote: Just use the count() function if your intent is to get the number of elements inside the exploded array. That loop is actually superfluous.

旁注:如果您的目的是获取爆炸数组中的元素数量,只需使用count()函数。那个循环实际上是多余的。

Note: You can device a way using for without using a count thru array pointers:

注意:您可以使用for设备,而无需使用通过数组指针计数:

$month_temp = "78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 81, 76, 73, 68, 72, 73, 75, 65, 74, 63, 67, 65, 64, 68, 73, 75, 79, 73";
$temp = explode(',', $month_temp);
for (current($temp); ($i = current($temp)) !== false; ($i = next($temp))) {
    // do something
    echo trim($i) , '<br/>';

}

Sample Output

#3


$i < $temp is not the condition you want. I'm not certain how PHP is doing the comparison, but you're comparing an integer to an array. That's always evaluating as true, so the loop continues for eternity.

$ i <$ temp不是你想要的条件。我不确定PHP如何进行比较,但是你将整数与数组进行比较。这总是评估为真,所以循环持续永恒。

Ironically, you want $i < count($temp)! That will let the loop run until $i is the same as the number of elements in the array, at which point you'll have three expressions ($i, $count and count($temp)) representing the number of elements in the array.

具有讽刺意味的是,你想要$ i ($>

So this is perhaps not the best way to test your for loop skills. Perhaps try running through a collection of integers to find the index of a particular number, to find the sum of all the numbers, the largest number, the smallest number, the range, the average, etc etc :)

所以这可能不是测试你的for循环技能的最佳方法。也许尝试运行整数集合来查找特定数字的索引,找到所有数字的总和,最大数字,最小数字,范围,平均值等等:)

#4


you can use sizeOf function in place of count like

您可以使用sizeOf函数代替计数

$max = sizeof($huge_array);
for($i = 0; $i < $max;$i++)
{
code...
}

If you do not want to use count or sizeof then it better you use

如果你不想使用count或sizeof那么你最好使用它

foreach($arr as $ar){
//Code here
}

Except this i do not thing there is any method available.

除此之外我没有任何方法可用。