PHP implode展开用于if else语句的逗号分隔值 [英] PHP implode explode comma separated values for use in if else statement
问题描述
我正在开发一个项目,我需要在数据库字段中输入逗号分隔值,然后提取要在if和else语句中使用的数据。
I am working on a project where I need to enter comma separated values in a database field and then extract the data to use in if and else statement.
示例:在数据库字段 user_status
中有 admin,admin1,admin2,admin3 ........等用户名
。我想使用这些值,如:
For example: in the database field user_status
i have usernames like admin,admin1,admin2,admin3........etc
. I want to use these values like:
$user_status = $row['user_status']; // database values of all the usernames
$username = $_SESSION['username']; // username of the logged in user eg. admin
if($user_status == $username) // if session username matches with a username in the database field
{
statements;
}else{
statements;
}
我想从数据库中提取逗号分隔的数据库值,登录用户的用户名与数据库中的许多用户名之一匹配。但我不知道如何做到这一点。请帮助我的专家。
I want to extract the comma separated database values from the database and match if the logged in user's username matches with one of many username's in the database. But I am not pretty sure how to do this. Please help me experts.
推荐答案
使用 explode
in_array
以检查 $ user_status
$ username > array。
Create an array with explode
and use in_array
to check if the $username
exists in the $user_status
array.
<?php
$username = $_SESSION['username'];
$user_status = explode(',', $row['user_status']);
if (in_array($username, $user_status)) {
//...
}
http://php.net/ manual / en / function.explode.php
http://php.net/manual/en/function.in-array.php
这篇关于PHP implode展开用于if else语句的逗号分隔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!