<?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);
}
}
?>
例如: id:1 身份2 id:3
user_id = 3的值,我不需要创建新的SESSION。
我想检查我的$ _SESSION中是否存在id。如果是这种情况,我不想创建新会话。我做错了什么 ?谢谢 。
答案 0 :(得分:0)
不是最迷人的解决方案,但您可以foreach
寻找它:
$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);
}
答案 1 :(得分:-1)
使用array[] = '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);
}
?>