LoginSignup
2

More than 5 years have passed since last update.

PHPで連想配列の先頭と末尾を取得 (keyが数字じゃなくても使える)

Posted at

どうでもいい小ネタ。

$last = end($array);
$first = reset($array);

順番をひっくり返すと副作用(内部ポインタズレ)が発生するので注意。
普通にコード書いてれば影響ないと思うけど、while使うような酔狂な人がいたら要注意。

keyも合わせて取得するならこんな感じ。

$last = end($array);
$last_key = key($array);
$first = reset($array);
$first_key = key($array);

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2