php - php 检查多个数组键值的真假
问题描述
我有一个类似下面的数组。
我需要检查 action_keys 的所有可能性以及 true 或 false 的值
尝试过如下方式,但需要知道在 PHP 中是否有任何优化的方法可以做到这一点
if( ($userInfoArray[0]['action_key'] =='loggedin' && $userInfoArray[0]['value']==1) && ($userInfoArray[1]['action_key'] =='optin' && $userInfoArray[1]['value']==1) ){
echo "LOGGEDIN and OPTIN---";
}
else if( ($userInfoArray[0]['action_key'] =='loggedin' && $userInfoArray[0]['value']==0) && ($userInfoArray[1]['action_key'] =='optin' && $userInfoArray[1]['value']==0) ){
echo "LOGOUT and OPTOUT---";
}
else if( ($userInfoArray[0]['action_key'] =='loggedin' && $userInfoArray[0]['value']==1) && ($userInfoArray[1]['action_key'] =='optin' && $userInfoArray[1]['value']==0) ){
echo "LOGGEDIN and Not Opted---";
}
else if( ($userInfoArray[0]['action_key'] =='loggedin' && $userInfoArray[0]['value']==0) && ($userInfoArray[1]['action_key'] =='optin' && $userInfoArray[1]['value']==1) ){
echo "LOGGEDOUT but Opted---";
}
问题是:
更新:
下面的数组键的 0 和 1 索引不断变化,例如有时 [0]['action_key'] 已登录,有时选择选项,并且值也可能与 0 或 1 不同。
所以我正在尝试条件下的所有组合。
像例子:
[0]['action_key']=loggedin and [0]['value']=0 and 1 ['action_key']=optin and 1 ['value']=0
[0]['action_key']=optin and [0]['value']=0 and and 1 ['action_key']=loggedin and 1 ['value']=1
[0]['action_key']=loggedin 和 [0]['value']=1 ...
以及所有可能的组合。
我使用的是:
if( ($userInfoArray[0]['action_key'] =='loggedin' && $userInfoArray[0]['value']==1) && ($userInfoArray[1]['action_key'] =='optin' && $userInfoArray[1]['value'] ==0 ) )
{}
else if( ($userInfoArray[0]['action_key'] =='optin' && $userInfoArray[0]['value']==0) && ($userInfoArray[1]['action_key'] =='loggedin' && $userInfoArray[1]['value'] ==1 ) )
{}
如果我尝试使用上面的代码,代码不会太长。
解决方案
最好的方法是避免 if 语句并在其他数组中定义我们的条件和消息。为了做到这一点,我们应该:
- 将我们的输入数组映射到表单
actionkey => actionvalue
,例如:"loggedin" => 0
- 在数组中指定我们的条件和满足条件时应打印的消息
- 过滤掉那些条件不符合我们输入值的消息
- 打印留下的消息
示例代码:
<?php
$input = [
[
'actionkey' => 'loggedin',
'value' => 1
],
[
'actionkey' => 'optin',
'value' => 0
]
];
//let's map it into actionkey => value
$actionValue = array_reduce(
$input,
function (array $out, $item) {
$out[$item['actionkey']] = $item['value'];
return $out;
},
[]
);
$flow = [
[
"when" => [ "loggedin" => 1, "optin" => 1 ],
"then" => "LOGGEDIN and OPTIN---"
],
[
"when" => [ "loggedin" => 0, "optin" => 0 ],
"then" => "LOGOUT and OPTOUT---"
],
[
"when" => [ "loggedin" => 1, "optin" => 0 ],
"then" => "LOGGEDIN and Not Opted---"
],
[
"when" => [ "loggedin" => 0, "optin" => 1 ],
"then" => "LOGGEDOUT but Opted---"
],
];
$messagesToPrint =
array_column(
array_filter(
$flow,
function (array $item) use ($actionValue) {
return empty(array_diff_assoc($item['when'], $actionValue));
}
),
"then"
);
echo implode("\n", $messagesToPrint);
推荐阅读
- python - TypeError: 'type' 和 'type' 的实例之间不支持 '<' 。对于 value_counts 方法
- python - 时间戳范围为小时 Python Pandas
- python - 如何在交叉验证期间获取实例索引
- java - 如何将图像保存到 SQLiteDatabase
- java - Java Reflection - 获取 JVM 进程中所有加载的类
- c++ - 检查字符串中的字符长度是否相同
- babeljs - Babel (@babel/types) JSDoc ast,打字在哪里?
- excel - 在 Excel VBA 中引用新工作簿
- python - 余额计算熊猫
- javascript - Gmail API 方法 users.history.list 发送不完整的消息对象作为响应