Get the first element of an array

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

    You can get the Nth element with a language construct, "list":

    // First item
    list($firstItem) = $yourArray;
    
    // First item from an array that is returned from a function
    list($firstItem) = functionThatReturnsArray();
    
    // Second item
    list( , $secondItem) = $yourArray;
    

    With the array_keys function you can do the same for keys:

    list($firstKey) = array_keys($yourArray);
    list(, $secondKey) = array_keys($yourArray);
    

提交回复
热议问题