我有10个学生ID,每个sub1
和sub2
定义了他们注册的主题,位于下面的$result
数组变量中
Array (
[0] => Array ( [sub1] => BIO [sub2] => )
[1] => Array ( [sub1] => BIO [sub2] => )
[2] => Array ( [sub1] => MATH [sub2] =>CHEM )
[3] => Array ( [sub1] => MATH [sub2] => BIO )
[4] => Array ( [sub1] => PHY [sub2] => )
[5] => Array ( [sub1] => ELCT [sub2] => )
[6] => Array ( [sub1] => SOC [sub2] => GEO)
[7] => Array ( [sub1] => HIS [sub2] => )
[8] => Array ( [sub1] => ART [sub2] => BIO )
[9] => Array ( [sub1] => PHY [sub2] => HIS )
)
和另一个$subjects
变量,其中包含每个学生应该注册的主题
Array (
[student1sub] => BIO
[student2sub] => MATH
[student3sub] => CHEM
[student4sub] => PHYS
[student5sub] => ELCT
[student6sub] => ECO
[student7sub] => SOC
[student8sub] => GEO
[student9sub] => HIS
[student10sub] => ART
)
我需要通过检查$subjects
和sub1
sub2
来检查每位学生是否参加了$result
条件中他应该注册的相应科目。 1}}数组
我知道我应该使用in_array
但不确定如何实现它以循环每个学生sub1
和sub2
if (in_array("BIO", $subject))
{
return true;
}
else
{
return false;
}
如果来自$result
的所有10名学生都满足其条件,那么如果没有显示错误,我需要显示成功消息msg
我想要实现的目标预期产出
在[student1sub]
[0] => Array ( [sub1] => BIO [sub2] => )
的子信息
在[student2sub]
[1] => Array ( [sub1] => BIO [sub2] => )
的子信息
在[student3sub]
[2] => Array ( [sub1] => MATH [sub2] =>CHEM )
的子信息
在[student4sub]
[3] => Array ( [sub1] => MATH [sub2] => BIO )
的子信息
在[student5sub]
[4] => Array ( [sub1] => PHY [sub2] => )
的子信息
在[student6sub]
[5] => Array ( [sub1] => ELCT [sub2] => )
的子信息
在[student7sub]
[6] => Array ( [sub1] => SOC [sub2] => GEO)
的子信息
在[student8sub]
[7] => Array ( [sub1] => HIS [sub2] => )
的子信息
在[student9sub]
[8] => Array ( [sub1] => ART [sub2] => BIO )
的子信息
在[student10sub]
[9] => Array ( [sub1] => PHY [sub2] => HIS )
的子信息
答案 0 :(得分:0)
您只想检查每位注册学生是否符合要求,所以:
// In the future, it would be helpful if you provide an array like this in the question:
$enrolled_student = array(
array('sub1' => 'BIO'),
array('sub1' => 'BIO', 'sub2' => 'CHEM'),
array('sub1' => 'MATH'),
array('sub1' => 'PHY', 'sub2' => 'GEO'),
array('sub1' => 'ART'),
array('sub1' => 'ELCT', 'sub2' => 'HIS')
);
// In the future, it would be helpful if you would provide an array like this in the question
$required = array(
'student1sub' => 'BIO',
'student2sub' => 'BIO',
'student3sub' => 'BIO',
'student4sub' => 'BIO',
'student5sub' => 'BIO',
'student6sub' => 'BIO'
);
$all_confirmed = TRUE;
$i = 0;
foreach( $required as $r )
{
if( in_array( $r, $enrolled_student[$i] ) )
{
// The requirement has been met
}
else
{
// The requirement has not been met
$all_confirmed = FALSE;
}
$i++;
}
echo $all_confirmed ? 'All confirmed' : 'Not all confirmed';
答案 1 :(得分:0)
我希望我理解你要做的事情。如果是这样,这可能对你有帮助。
$result=array (
array('sub1' => 'BIO' ,'sub2' =>''),
array( 'sub1' => 'BIO' ,'sub2' =>'' ),
array( 'sub1' => 'MATH', 'sub2' =>'CHEM' ),
array( 'sub1' => 'MATH', 'sub2' => 'BIO' ),
array( 'sub1' => 'PHY' ,'sub2' =>'' ),
array( 'sub1' => 'ELCT', 'sub2' =>'' ),
array( 'sub1' => 'SOC' ,'sub2' => 'GEO'),
array( 'sub1' => 'HIS' ,'sub2' =>'' ),
array( 'sub1' => 'ART' ,'sub2' => 'BIO' ),
array( 'sub1' => 'PHY' ,'sub2' => 'HIS' ),
);
$subjects=array(
'student1sub' => 'BIO',
'student2sub' => 'MATH',
'student3sub' => 'CHEM',
'student4sub' => 'PHYS',
'student5sub' => 'ELCT',
'student6sub' => 'ECO',
'student7sub' => 'SOC',
'student8sub' => 'GEO',
'student9sub' => 'HIS',
'student10sub' => 'ART'
);
$i=0;
$test=true;
//bool in_array ( mixed $needle , array $haystack , bool $strict = FALSE )
foreach ($subjects as $val){
echo "\n student ".($i+1)." is studying ".$result[$i]["sub1"]." and ".$result[$i]["sub2"]." he should be studying ".$val;
if (!in_array($val,$result[$i])){
$test=false;
break;
}
$i++;
}
if ($test)
echo "\n success";
else
echo "\n error";