LoginSignup
4
4

More than 5 years have passed since last update.

PHPで配列のキーにintにキャストできる文字列を指定するとintになる

Last updated at Posted at 2017-07-12

仕様です

PHP: 配列 - Manual

integer として妥当な形式の文字列は integer 型にキャストされます。 つまり、キーに "8" を指定すると、実際には 8 として格納されるということです。一方 "08" はキャストされません。これは十進数として妥当な形式ではないからです。

なお、他にもいくつかキャストの例が載っています。PHPのリファレンス充実してる。

実行環境

PHPのREPL PsySHで実行(PHP5.6。PHP7でも同様の結果)

実行結果

>>>  $a['+1']['-2']['3']["4+5"]=['6']
=> [
     "6", # 値はもちろん文字列
   ]
>>> $a
=> [
     "+1" => [
       -2 => [ # string -> int
         3 => [ # string -> int
           "4+5" => [
             "6",
           ],
         ],
       ],
     ],
   ]
4
4
6

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
4
4