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

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

    You would use $arr[ 0 ]; array_shift removes the first element from the array.

    EDIT


    This answer is actually somewhere between incomplete and plain out wrong but, because the comments of the two jon's I think that it should actually stay up so that others can see that discourse.

    The right answer:

    • reset is the method to return the first defined index of the array. Even in non-associative arrays, this may not be the 0 index.
    • array_shift will remove and return the value which is found at reset

    The OP made the assumption that $arr[0] is the first index is not accurate in that particular context.

提交回复
热议问题