不确定在这种情况下我是否可以使用 array_intersect 或 array_search
Not sure if I can use array_intersect or array_search in this case
我有一个数组 ($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
没有找到密钥,函数 returns FALSE
然后它将检查 OR
语句的右侧。这是不正确的吗?我看到 array_intersect
我认为可能有效,但它看起来不适用于具有关联键的数组。
与 JavaScript 不同,||
运算符总是 returns 布尔值。将其替换为 ?:
运算符。
$a ?: $b
实际上是 $a ? $a : $b
的缩写语法,参见 ternary operator:
The expression (expr1) ? (expr2) : (expr3)
evaluates to expr2
if expr1
evaluates to TRUE
, and expr3
if expr1
evaluates to FALSE
.
Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3
returns expr1
if expr1
evaluates to TRUE
, and expr3
otherwise.
您可以使用 array_intersect_key 来获取您正在寻找的值。它 returns 一个数组。您可以使用重置获取结果数组的第一个(理论上)元素。它将给出严格的标准通知 "Only variables should be passed by reference" 但它会起作用。
$first = reset(array_intersect_key($entry, ['custom_0_first' => 0, 'custom_1_first' => 0]));
$last = reset(array_intersect_key($entry, ['custom_0_last' => 0, 'custom_1_last' => 0]));
另一种方法是使用 isset 检查密钥。
$first = isset($entry['custom_0_first']) ? $entry['custom_0_first'] : $entry['custom_1_first'];
$last = isset($entry['custom_0_last']) ? $entry['custom_0_last'] : $entry['custom_1_last'];
我有一个数组 ($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
没有找到密钥,函数 returns FALSE
然后它将检查 OR
语句的右侧。这是不正确的吗?我看到 array_intersect
我认为可能有效,但它看起来不适用于具有关联键的数组。
与 JavaScript 不同,||
运算符总是 returns 布尔值。将其替换为 ?:
运算符。
$a ?: $b
实际上是 $a ? $a : $b
的缩写语法,参见 ternary operator:
The expression
(expr1) ? (expr2) : (expr3)
evaluates toexpr2
ifexpr1
evaluates toTRUE
, andexpr3
ifexpr1
evaluates toFALSE
.Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression
expr1 ?: expr3
returnsexpr1
ifexpr1
evaluates toTRUE
, andexpr3
otherwise.
您可以使用 array_intersect_key 来获取您正在寻找的值。它 returns 一个数组。您可以使用重置获取结果数组的第一个(理论上)元素。它将给出严格的标准通知 "Only variables should be passed by reference" 但它会起作用。
$first = reset(array_intersect_key($entry, ['custom_0_first' => 0, 'custom_1_first' => 0]));
$last = reset(array_intersect_key($entry, ['custom_0_last' => 0, 'custom_1_last' => 0]));
另一种方法是使用 isset 检查密钥。
$first = isset($entry['custom_0_first']) ? $entry['custom_0_first'] : $entry['custom_1_first'];
$last = isset($entry['custom_0_last']) ? $entry['custom_0_last'] : $entry['custom_1_last'];