Php 检查数组中的数组值
我使用此代码检查数组中是否存在一个值:Php 检查数组中的数组值,php,Php,我使用此代码检查数组中是否存在一个值: <?php if (in_array($_SESSION['bedrijf'], array( "torza", "thure", "mb" )) && in_array($_SESSION['user_type'], array( "beheerder", "administratie", "kantoor", "werkplaats" ))) { echo 'exec
<?php
if (in_array($_SESSION['bedrijf'], array(
"torza",
"thure",
"mb"
)) && in_array($_SESSION['user_type'], array(
"beheerder",
"administratie",
"kantoor",
"werkplaats"
))) {
echo 'execute script';
}
?>
但是现在脚本没有执行,或者没有给出任何错误
print\r($\u SESSION['bedrijf'])
givesArray([0]=>mb[1]=>thure[2]=>torza)
print\r($\u SESSION['user\u type'])
givesbeheerder
您将无法在草堆中找到针阵列作为子阵列。如果$\u会话['bedrijf']
始终是一个数组,则检查交叉点(至少1个匹配项):
由于您的
$\u会话['bedrijf']
包含一个数组,因此可以使用以下代码获得结果
<?php
$allowScript = false;
if(is_array($_SESSION['bedrijf']))
{
foreach($_SESSION['bedrijf'] as $bedrijf)
{
if (in_array($bedrijf, array("torza","thure","mb")) && in_array($_SESSION['user_type'], array("beheerder", "administratie", "kantoor","werkplaats")
{
$allowScript = true;
// Execute Script!
}
}
}
if($allowScript === TRUE)
{
// Execute script
}
else
{
// Not allowed
}
?>
你真的没有给我们太多的机会。没有报告错误?两个会话都已设置吗?你有什么错误吗?您是否已确保提供了一份?有关详细信息,请参阅有关的帮助文章。如果语句看起来不错,是否确定$\u会话变量设置正确?“$\u会话['bedrijf']”始终是arrayarray\u intersect返回数组。不是整数。用count()将其换行。
交点是3是数组的值数,对吗?每个值的值数可以不同user@Muiter你到底在找什么?您只是想知道是否所有的值都存在吗?或者,如果存在任何值,则删除==3
,因此如果存在任何值,或>0
。如果存在任何值,则可能需要在找到时添加中断。
<?php
$allowScript = false;
if(is_array($_SESSION['bedrijf']))
{
foreach($_SESSION['bedrijf'] as $bedrijf)
{
if (in_array($bedrijf, array("torza","thure","mb")) && in_array($_SESSION['user_type'], array("beheerder", "administratie", "kantoor","werkplaats")
{
$allowScript = true;
// Execute Script!
}
}
}
if($allowScript === TRUE)
{
// Execute script
}
else
{
// Not allowed
}
?>