Get the first element of an array

后端 未结 30 2410
醉酒成梦
醉酒成梦 2020-11-22 10:59

I have an array:

array( 4 => \'apple\', 7 => \'orange\', 13 => \'plum\' )

I would like to get the first element of this array. Expect

30条回答
  •  不要未来只要你来
    2020-11-22 11:31

    I don't like fiddling with the array's internal pointer, but it's also inefficient to build a second array with array_keys() or array_values(), so I usually define this:

    function array_first(array $f) {
        foreach ($f as $v) {
            return $v;
        }
        throw new Exception('array was empty');
    }
    

提交回复
热议问题