首页 > 解决方案 > 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 )   )
    {}

如果我尝试使用上面的代码,代码不会太长。

标签: php

解决方案


最好的方法是避免 if 语句并在其他数组中定义我们的条件和消息。为了做到这一点,我们应该:

  1. 将我们的输入数组映射到表单actionkey => actionvalue,例如:"loggedin" => 0
  2. 在数组中指定我们的条件和满足条件时应打印的消息
  3. 过滤掉那些条件不符合我们输入值的消息
  4. 打印留下的消息

示例代码:

<?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);

推荐阅读