There is an array of user states stored in the session. This works:
会话中存储了一个用户状态数组。如此:
<?php if ($_SESSION['_app_user']['data']['state']['1']) { ?>
<p>User has state 1</p>
<?php } ?>
But, selecting multiple states doesn't:
但是,选择多个州并不:
<?php if ($_SESSION['_app_user']['data']['state']['1,6,10']) { ?>
<p>User has state 1 or 6 or 10</p>
<?php } ?>
How can you check on multiple states?
如何检查多个状态?
3 个解决方案
#1
4
By checking multiple.
通过检查多个。
You may find it easier to store the least common denominator to a temporary variable:
您可能会发现将最小公分母存储到临时变量中更容易:
$s = $_SESSION['_app_user']['data']['state'];
if(isset($s[1]) || isset($s[6]) || isset($s[10])) {
echo 'Tahdah!';
}
unset($s);
Also, please use quotes for your strings. It makes code clearer, and saves the PHP interpreter a bit of effort guessing that you mean a string instead of, say, a constant named _app_user
:)
另外,请对字符串使用引号。它使代码更清晰,并节省了PHP解释器的一些工作,使其猜测您的意思是字符串,而不是名为_app_user:的常量)
#2
1
May be it's better to use "array_key_exists" function to check if the given index exists in the array. See Example #2 array_key_exists() vs isset(). http://bg2.php.net/manual/en/function.array-key-exists.php
也许最好使用“array_key_exists”函数检查给定的索引是否存在于数组中。参见示例#2 array_key_exists()和isset()。http://bg2.php.net/manual/en/function.array-key-exists.php
#3
1
You could also use array_intersect
to check an array of states against your user states. For example:
您还可以使用array_intersect来检查针对用户状态的状态数组。例如:
$user_states = $_SESSION['_app_user']['data']['state'];
$check_states = array( 1, 6, 10 );
$matches = array_intersect(array_keys($user_states), $check_states);
if(!empty($matches))
{
echo "User has valid states: \n";
foreach($matches as $_state)
{
echo " - {$_state}\n";
}
}
else
{
echo "Sorry. Not found.";
}
The function checks whether any two elements in the arrays match, and returns all the matches. Meaning that in that code, the $matches
array would be a list of all the states that the user has and are in your list.
函数检查数组中的任何两个元素是否匹配,并返回所有匹配。这意味着,在该代码中,$matches数组将是用户拥有和在列表中的所有状态的列表。
#1
4
By checking multiple.
通过检查多个。
You may find it easier to store the least common denominator to a temporary variable:
您可能会发现将最小公分母存储到临时变量中更容易:
$s = $_SESSION['_app_user']['data']['state'];
if(isset($s[1]) || isset($s[6]) || isset($s[10])) {
echo 'Tahdah!';
}
unset($s);
Also, please use quotes for your strings. It makes code clearer, and saves the PHP interpreter a bit of effort guessing that you mean a string instead of, say, a constant named _app_user
:)
另外,请对字符串使用引号。它使代码更清晰,并节省了PHP解释器的一些工作,使其猜测您的意思是字符串,而不是名为_app_user:的常量)
#2
1
May be it's better to use "array_key_exists" function to check if the given index exists in the array. See Example #2 array_key_exists() vs isset(). http://bg2.php.net/manual/en/function.array-key-exists.php
也许最好使用“array_key_exists”函数检查给定的索引是否存在于数组中。参见示例#2 array_key_exists()和isset()。http://bg2.php.net/manual/en/function.array-key-exists.php
#3
1
You could also use array_intersect
to check an array of states against your user states. For example:
您还可以使用array_intersect来检查针对用户状态的状态数组。例如:
$user_states = $_SESSION['_app_user']['data']['state'];
$check_states = array( 1, 6, 10 );
$matches = array_intersect(array_keys($user_states), $check_states);
if(!empty($matches))
{
echo "User has valid states: \n";
foreach($matches as $_state)
{
echo " - {$_state}\n";
}
}
else
{
echo "Sorry. Not found.";
}
The function checks whether any two elements in the arrays match, and returns all the matches. Meaning that in that code, the $matches
array would be a list of all the states that the user has and are in your list.
函数检查数组中的任何两个元素是否匹配,并返回所有匹配。这意味着,在该代码中,$matches数组将是用户拥有和在列表中的所有状态的列表。