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

前端 未结 11 725
说谎
说谎 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:38

    Do you want to modify the arr array also? array_shift removes the first element of the array and returns it, thus the array has changed. $arr[0] merely gives you the first element.

    I would use $arr[0] unless I explicitly wanted to modify the array. You may add code later to use the arr array and forget that it was modified.

提交回复
热议问题