Arrays: array_shift($arr) or $arr[0]?

前端 未结 11 687
说谎
说谎 2021-01-14 04:08

Which one would you use?

Basically I only want to get the 1st element from a array, that\'s it.

11条回答
  •  庸人自扰
    2021-01-14 04:44

    Well, they do different things.

    • array_shift($arr) takes the first element out of the array, and gives it to you.

    • $arr[0] just gives it to you... if the array has numeric keys.

    An alternative that works for associative arrays too is reset($arr). This does move the array's internal pointer, but unless you're using those functions this is unlikely to affect you.

提交回复
热议问题