如何比较2个数组的值,如果所有条件都为真,则返回true

时间:2017-08-23 18:05:14

标签: php arrays

我有10个学生ID,每个sub1sub2定义了他们注册的主题,位于下面的$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 
    )

我需要通过检查$subjectssub1 sub2来检查每位学生是否参加了$result条件中他应该注册的相应科目。 1}}数组

我知道我应该使用in_array但不确定如何实现它以循环每个学生sub1sub2

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 )的子信息

2 个答案:

答案 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";