strict false
以下のように数値とオブジェクトが混在する連想配列からオブジェクトを探そうとするとNoticeエラーが出る.
array_test.php
<?php
$sample = new stdClass;
$arr = [
'age' => 10,
'name' => 'kappa',
'object' => $sample,
];
// $arr配列の中から$sampleオブジェクトを検索
var_dump( in_array($sample, $arr) );
var_dump( array_search($sample, $arr) );
php5.5ではNoticeが出る
$ /usr/local/php-5.5.4/bin/php array_search_test.php
PHP Notice: Object of class stdClass could not be converted to int in /home/vagrant/work/array_search_test.php on line 9
PHP Notice: Object of class stdClass could not be converted to int in /home/vagrant/work/array_search_test.php on line 10
Notice: Object of class stdClass could not be converted to int in /home/vagrant/work/array_search_test.php on line 9
bool(true)
Notice: Object of class stdClass could not be converted to int in /home/vagrant/work/array_search_test.php on line 10
string(6) "object"
php5.6では何も出ない
$ /usr/local/php-5.6.0/bin/php array_search_test.php
bool(true)
string(6) "object"
strict true
$strict=trueを指定すればphp5.5, php5.6どちらでもNoticeを消すことができる.
http://php.net/manual/ja/function.array-search.php
<?php
$sample = new stdClass;
$arr = [
'age' => 10,
'name' => 'kappa',
'object' => $sample,
];
// $arr配列の中から$sampleオブジェクトを検索
var_dump( in_array($sample, $arr, /*$strict=*/ true) );
var_dump( array_search($sample, $arr, /*$strict=*/ true) );
php5.5(Notice解消)
$ /usr/local/php-5.5.4/bin/php array_search_test.php
bool(true)
string(6) "object"
php5.6
$ /usr/local/php-5.6.0/bin/php array_search_test.php
bool(true)
string(6) "object"
phpの型変換はややこしいので、$strict=trueを指定したほうがよさそうです.