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

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

    $arr[0] only works if the array as numerical keys.

    array_shift removes the element from the array and it modifies the array itself.

    If you are not sure what the first key is , and you do not want to remove it from the array, you could use:

    $v){
       $value = $v;
       break;
    }
    

    or even better:

提交回复
热议问题