为什么 in_array returns 以 Object::Class 为键为假?
Why does in_array returns false with Object::Class as key?
这很奇怪,我不明白为什么。
我有这个数组:
$exludedAction = [UserController::class => [
"login", "register"
]
];
当我检查密钥是否为 in_array 时 returns false:
$type = get_class($userControllerObject);
$cl = in_array($type, $exludedAction); // is false
我检查严格比较:
$c = $type === UserController::class; // returns true
请注意 UserController::class
位于命名空间内:App\namespace\class
.
并检查 isset
returns 是否正确:
isset($exludedAction[$type]); // returns true
in_array
searches an array for values, not keys. You should use array_key_exists
改为:
$exludedAction = [UserController::class => [
"login", "register"
]
];
$type = get_class($userControllerObject);
$cl = array_key_exists($type, $exludedAction);
var_dump($cl);
输出:
bool(true)
这很奇怪,我不明白为什么。
我有这个数组:
$exludedAction = [UserController::class => [
"login", "register"
]
];
当我检查密钥是否为 in_array 时 returns false:
$type = get_class($userControllerObject);
$cl = in_array($type, $exludedAction); // is false
我检查严格比较:
$c = $type === UserController::class; // returns true
请注意 UserController::class
位于命名空间内:App\namespace\class
.
并检查 isset
returns 是否正确:
isset($exludedAction[$type]); // returns true
in_array
searches an array for values, not keys. You should use array_key_exists
改为:
$exludedAction = [UserController::class => [
"login", "register"
]
];
$type = get_class($userControllerObject);
$cl = array_key_exists($type, $exludedAction);
var_dump($cl);
输出:
bool(true)