How can I get the current array index in a foreach loop?

前端 未结 10 1734
陌清茗
陌清茗 2020-12-14 06:19

How do I get the current index in a foreach loop?

foreach ($arr as $key => $val)
{
    // How do I get the index?
    // How do I get the fir         


        
10条回答
  •  长情又很酷
    2020-12-14 06:31

    well since this is the first google hit for this problem:

    function mb_tell(&$msg) {
        if(count($msg) == 0) {
            return 0;
        }
        //prev($msg);
        $kv = each($msg);
        if(!prev($msg)) {
            end($msg);
    
            print_r($kv);
            return ($kv[0]+1);
        }
        print_r($kv);
        return ($kv[0]);
    }
    

提交回复
热议问题