doumiang2297 2014-04-27 16:03
浏览 33
已采纳

在多维数组中foreach循环中的奇怪行为

THE QUESTION

Why is the line with "l++;" impacting the loop counts of the foreach loop? That line is merely there as a counter but somehow it is impacting the end result.

Can someone please help explain what is going on here?

THE CODE

I have the following multidimensional array:

$team_array = array(
    array(68, 6, 0, 10, "1.000"),
    array(72, 6, 0, 10, "1.000"),
    array(65, 6, 0, 8, "1.000"),
    array(62, 6, 0, 4, "1.000"),
    array(54, 3, 3, 3, ".500"),
    array(55, 3, 3, -5, ".500"),
    array(59, 0, 6, -16, ".000")
);

I am using the following code to loop over the arrays.

$foo = "1.000";    
$k = 0;
$l = 0;
$m = 0;
$n = 0;
foreach($team_array as $index => $inner_arr)
{
    foreach($inner_arr as $value)
    //echo $value.", ";
    //echo "<br/>";
    $l++;
    { 
        $m++;
        echo "index - $index<br>";
        echo "perc = ".$team_array[$index][4]."<br>";
        if ($team_array[$index][4] == $foo)
        {
            $tie_tm[] = $team_array[$index][0];
            $n++;
        }   

    }

    $k++;
}
echo "k - $k<br>";
echo "l - $l<br>";
echo "m - $m<br>";
echo "n - $n<br>";

echo "<pre>";
print_r($tie_tm);

I have left the debugging bits in as they show the strange (to me) behavior. Specifically, if I comment out the line "l++;" I get LOTS more loops than I would expect. Running this code without the "l++;" line give me this:

k - 7
l - 0
m - 35
n - 20
Array
(
[0] => 68
[1] => 68
[2] => 68
[3] => 68
[4] => 68
[5] => 72
[6] => 72
[7] => 72
[8] => 72
[9] => 72
[10] => 65
[11] => 65
[12] => 65
[13] => 65
[14] => 65
[15] => 62
[16] => 62
[17] => 62
[18] => 62
[19] => 62
)

With it left in, I get:

k - 7
l - 35
m - 7
n - 4
Array
(
[0] => 68
[1] => 72
[2] => 65
[3] => 62
)

This is the result I want and expected.

In the end, what I need is exactly what I am getting in the final result with 4 elements in the array. I would like to remove the debugging bits but need to understand how I might need to alter this code to keep my end result.

Thanks in advance!

  • 写回答

4条回答 默认 最新

  • dongshuzhuo5659 2014-04-27 16:11
    关注

    Reframed your code a bit. Try -

    $team_array = array(
            array(68, 6, 0, 10, "1.000"),
            array(72, 6, 0, 10, "1.000"),
            array(65, 6, 0, 8, "1.000"),
            array(62, 6, 0, 4, "1.000"),
            array(54, 3, 3, 3, ".500"),
            array(55, 3, 3, -5, ".500"),
            array(59, 0, 6, -16, ".000")
          );
    
    $result = array();
    for($i=0; $i<4; $i++)
    {
        $result[] = $team_array[$i][0];
    }
    
    print_r($result);
    


    OUTPUT

    Array
    (
       [0] => 68
       [1] => 72
       [2] => 65
       [3] => 62
    )
    


    DEMO

    http://3v4l.org/RJu69#v430

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭