Count number of iterations in a foreach loop

后端 未结 10 2042
礼貌的吻别
礼貌的吻别 2020-12-13 03:41

How to calculate how many items in a foreach?

I want to count total rows.

foreach ($Contents as $item) {
    $item[number];// if there are 15 $item[n         


        
10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 03:55

    foreach ($array as $value)
    {       
        if(!isset($counter))
        {
            $counter = 0;
        }
        $counter++;
    }
    

    //Sorry if the code isn't shown correctly. :P

    //I like this version more, because the counter variable is IN the foreach, and not above.

提交回复
热议问题