Return first key of associative array in PHP

前端 未结 5 1763
日久生厌
日久生厌 2020-12-13 06:24

I\'m trying to obtain the first key of an associative array, without creating a temporary variable via array_keys() or the like, to pass by reference. Unfortuna

5条回答
  •  粉色の甜心
    2020-12-13 06:54

    Although array_shift(array_keys($array)); will work, current(array_keys($array)); is faster as it doesn't advance the internal pointer.

    Either one will work though.

    Update

    As @TomcatExodus noted, array_shift(); expects an array passed by reference, so the first example will issue an error. Best to stick with current();

提交回复
热议问题