Get the first element of an array

后端 未结 30 2089
醉酒成梦
醉酒成梦 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:24

    Use:

    $first = array_slice($array, 0, 1);  
    $val= $first[0];
    

    By default, array_slice does not preserve keys, so we can safely use zero as the index.

提交回复
热议问题