【发布时间】:2015-06-30 18:34:41
【问题描述】:
我有一个数组 ($entry),它可以有两组键中的任何一组:
"custom_0_first" AND "custom_0_last";
或
"custom_1_first" AND "custom_1_last";
我正在尝试执行以下操作,但似乎没有设置变量:
$firstname = array_search('custom_0_first', $entry) || array_search('custom_1_first', $entry);
$lastname = array_search('custom_0_last', $entry) || array_search('custom_1_last', $entry);
请注意,$entry['custom_0_first'] 确实可以正常工作。我试图在这里避免使用 IF 语句。
我对 array_search 或 PHP 工作原理的理解是否不正确?据我了解,如果第一个array_search 没有找到密钥,则该函数返回FALSE,然后它将检查OR 语句的右侧。这是不正确的吗?我看到了array_intersect,我认为它可能有效,但它似乎不适用于具有关联键的数组。
【问题讨论】:
-
请显示 var_dump($entry)
-
array_search() 找到值,而不是键
-
@splash58 是的,我想要的是值,而不是键。如果键存在,我想要属于
custom_0_first的值,否则属于custom_1_first的值 -
可能我理解的不正确,但是如果
'custom_0_first'、'custom_0_last'等可能是$entry的keys,那么array_search()就找不到了他们。 -
@Don'tPanic 你是对的。我误解了array_search。我虽然它正在搜索一个键,如果找到该键,将返回它的值。看起来我要回到过去的
isset和?:。
标签: php arrays if-statement operators