我有一个类似下面的数组。
我需要检查action_keys的所有可能性以及值是否为真
尝试如下所示,但需要知道是否有任何优化的方法可以在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']有时选择optin一样,值也可能在0或1之间。
所以我正在尝试条件下的所有组合。
例如示例:
[0] ['action_key'] =已登录且[0] ['value'] = 0和1 ['action_key'] = optin且1 ['value'] = 0
[0] ['action_key'] = optin和[0] ['value'] = 0以及1 ['action_key'] =已登录和1 ['value'] = 1
[0] ['action_key'] =已登录,[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 ) )
{}
如果我尝试上面使用过的代码,代码不会太长。
答案 0 :(得分: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);
答案 1 :(得分:1)
如果希望所有可能性,并且键可以在0和1之间变化,则可以使用array_column并将triggers {
pollSCM '* * * * *'
}
checkout changelog: false, scm: [$class: 'GitSCM', branches: [[name: 'refs/heads/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [...]
checkout changelog: false, scm: [...]
checkout changelog: false, scm: [...]
用作第二个参数,以使该值成为键而不是0或1。
例如,没有action_key
和and
变体的示例:
but
那样,您就可以构造您的消息。
答案 2 :(得分:1)
如果您打算执行其他操作而不只是打印消息,则建议对已排序的输入数组的选定元素使用开关。
// Usage with ActiveForm and model
echo $form->field($model, 'LecturerName')->widget(Select2::classname(), [
'data' => ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname'),
'options' => ['placeholder' => 'Select Lecturer'],
'pluginOptions' => [
'allowClear' => true
],
'pluginEvents'=>[
"select2:select" => 'function() { $("#{$ddId}").on('change',function(e){
$("#{$inputId}").val($(this).val());
}); }',
]
]);
通过这种方式,sort($userInfoArray);
switch (array_column($userInfoArray, 'value', 'action_key')) {
case ['loggedin' => 1, 'optin' => 1]:
echo "LOGGEDIN and OPTIN---";
break;
case ['loggedin' => 0, 'optin' => 0]:
echo "LOGOUT and OPTOUT---";
break;
case ['loggedin' => 1, 'optin' => 0]:
echo "LOGGEDIN and Not Opted---";
break;
case ['loggedin' => 0, 'optin' => 1]:
echo "LOGGEDOUT but Opted---";
break;
}
可以排在第一位,optin
其次,并且仍然可以正确匹配。
loggedin
输出:
$userInfoArray = [
[
'action_key' => 'optin',
'value' => 0,
],
[
'action_key' => 'loggedin',
'value' => 1,
],
];
答案 3 :(得分:0)
我不能保证这是正确的代码。但是我认为下面的代码会给您解决问题的想法
<?php
$array = array(
0 => array(
'actionkey' => 'loggedin',
'value' => 1
),
1 => array(
'actionkey' => 'optin',
'value' => 0
)
);
foreach ($array as $key => $value){
foreach ($value as $keyy => $data) {
echo $keyy;
echo "<br>";
}
echo "<br>";
}
?>