drzablspw01655860 2014-05-20 16:46
浏览 48

数组和in_array

I am wondering if it is possible to search an array, with another array's values. So if there is 2 arrays, Array a and Array b - array a will see if its values as any results in array b.

define('L001', 'Wrong Password');
define('L002', 'Form not filled in');
define('L003', 'Account is not active');

$errors = array ('L001', 'L002', 'L003');
$args = explode('/', rtrim($_SERVER['QUERY_STRING'], '/'));

if (isset($args) && in_array($errors, $args)) {
    if (in_array($errors[0], $args)) {
        $error = L001;
    } elseif (in_array($errors[1], $args)) {
        $error = L002;
    } elseif (in_array($errors[2], $args)) {
        $error = L003;
    }
} else {
//no errors
}

Is something like this possible?

  • 写回答

2条回答 默认 最新

  • dssj88098 2014-05-20 17:05
    关注

    The code above doesn't work in my head. Additionally, in_array() will only work on whole arrays, not an element of one. More sensible would be:

    $errors = array('L001' => 'Wrong Password', 
                    'L002' => 'Form not filled in',
                    'L003' => 'Account is not active');
    
    if(isset($args) && in_array($args, $errors)) {
        echo $errors[$args]; // will output the text
    } else {
    // no errors
    }
    

    You could alternatively assign $errors[$args] to a variable for later use if you don't want to output it right there and then with $error = $errors[$args];

    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答