Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 查找会话数组中是否存在密钥_Php_Session - Fatal编程技术网

Php 查找会话数组中是否存在密钥

Php 查找会话数组中是否存在密钥,php,session,Php,Session,例如: 身份证号码:1 id 2 身份证号码:3 user_id=3的值,我不需要创建新会话 我想检查我的$\会话中是否存在该id。如果是这样,我不想创建新会话。我做错了什么?谢谢 使用数组[]='hello';用于将元素添加到数组中,要读取它,不使用[],而是使用元素的当前索引 <?php // i have multiple id and user_name in my SESSION, i don't want to create a new session if 5 exi

例如: 身份证号码:1 id 2 身份证号码:3

user_id=3的值,我不需要创建新会话

我想检查我的$\会话中是否存在该id。如果是这样,我不想创建新会话。我做错了什么?谢谢

使用数组[]='hello';用于将元素添加到数组中,要读取它,不使用[],而是使用元素的当前索引

    <?php
// i have multiple id and user_name in my SESSION, i don't want to create a new session if 5 exist.
    if($_SESSION['paneluser'][]['user_id'] != $user_id) {
        $_SESSION['paneluser'][] = array('user_id' => $user_id, 'user_name' => $user_name);
        }
    }
    ?>

这不是最吸引人的解决方案,但您可以尝试寻找:

   <?php
    // i have multiple id and user_name in my SESSION, i don't want to create a new session if 5 exist.
    if($_SESSION['paneluser']['user_id'] != $user_id) {
        $_SESSION['paneluser'] = array('user_id' => $user_id, 'user_name' => $user_name);
    }
    ?>
为什么继续而不中断?
$found = false;
foreach($_SESSION['paneluser'] as $user) {
    if($user['user_id'] == $user_id) { // look for the user ID in session array
        $found = true; // set your flag if its found
        break; // found what you want, no need to continue the loop
    }
}

if(!$found) { // if you didn't find it
    // create new entry
    $_SESSION['paneluser'][] = array('user_id' => $user_id, 'user_name' => $user_name);
}