LoginSignup
1

More than 3 years have passed since last update.

php7.2からeach()関数は非推奨になった

Last updated at Posted at 2019-10-31

この関数は、配列のkeyとvalueのペアを返す関数ですが、
foreachよりもループが遅く、PHPそのものの変更に伴って実装上の問題を引き起こしたことから非推奨になったそうです。

通常はlist関数と併用することが多いですが、普通にforeachを使えば十分なのであまり困ることはないかと思います。

// 関数サンプル

$foo = ["test1", "test2", "test3", "test4"];
$bar = each($foo);
var_dump($bar);
出力結果
ーーーーー
array(4) {
  [1]=>
  string(5) "test1"
  ["value"]=>
  string(5) "test1"
  [0]=>
  int(0)
  ["key"]=>
  int(0)
}

なお、PhpStorm(本環境は2019.2)上では下記のように非推奨関数として打ち消し線が表示されています。

スクリーンショット 2019-10-31 11.23.58.png

https://www.php.net/manual/ja/migration72.deprecated.php
https://www.php.net/manual/ja/function.each.php

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
What you can do with signing up
1