[英]Why does in_array returns false with Object::Class as key?
這很奇怪,我不明白為什么。
我有這個數組:
$exludedAction = [UserController::class => [
"login", "register"
]
];
當我檢查密鑰是否為 in_array 時,它返回 false:
$type = get_class($userControllerObject);
$cl = in_array($type, $exludedAction); // is false
我檢查嚴格比較: $c = $type === UserController::class; // returns true
$c = $type === UserController::class; // returns true
請注意, UserController::class
位於命名空間內: App\namespace\class
。
並檢查isset
返回 true: isset($exludedAction[$type]); // returns true
isset($exludedAction[$type]); // returns true
in_array
在數組中搜索值,而不是鍵。 您應該改用array_key_exists
:
$exludedAction = [UserController::class => [
"login", "register"
]
];
$type = get_class($userControllerObject);
$cl = array_key_exists($type, $exludedAction);
var_dump($cl);
Output:
bool(true)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.