3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class Foo { const X = [ 'code' => 101, 'reason' => 'reason 101', ]; const Y = [ 'code' => 102, 'reason' => 'reason 102', ]; public function getFooConstant($code) { $class = new \ReflectionClass(__CLASS__); $constants = $class->getConstants(); var_dump( $constants ); return array_filter($constants, function($v, $k) use ($code) { return $v['code'] == $code; }, ARRAY_FILTER_USE_BOTH); } } $foo = new Foo(); var_dump( $foo->getFooConstant(101) );
based on rvoiB
Output for 5.6.0 - 7.3.1
array(2) { ["X"]=> array(2) { ["code"]=> int(101) ["reason"]=> string(10) "reason 101" } ["Y"]=> array(2) { ["code"]=> int(102) ["reason"]=> string(10) "reason 102" } } array(1) { ["X"]=> array(2) { ["code"]=> int(101) ["reason"]=> string(10) "reason 101" } }