Php 查找会话数组中是否存在密钥
例如: 身份证号码:1 id 2 身份证号码:3 user_id=3的值,我不需要创建新会话 我想检查我的$\会话中是否存在该id。如果是这样,我不想创建新会话。我做错了什么?谢谢 使用数组[]='hello';用于将元素添加到数组中,要读取它,不使用[],而是使用元素的当前索引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
<?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);
}