Findlay Mack
Findlay Mack

Reputation: 25

trying to match a value in two arrays, if a matched value is present then output true

trying to match a value in two arrays, if a matched value is present then output true, thinking maybe the array_intersect function? very unsure! Any help is much appreciated!

Basically have two sql queries which i cant quite fit into this box! but they each return an array $staffExpertise and $moduleExpertise, but im very new to php and even more unfamiliar with the array intersect function so not too sure what to do from here!

    foreach ($results as $row) {
        $staffExpertise = $row['expertise'];
    }

    foreach ($results as $row) {
        $moduleExpertise = $row['expertise'];
    }

    $arrayIntersection = array_intersect($moduleExpertise,   $staffExpertise);

    if($arrayIntersection = ){

    }

Upvotes: 2

Views: 5633

Answers (2)

AaronSantos
AaronSantos

Reputation: 159

Yes array intersect does it. Try something like this:

$names_1 = array("Alice", "Bob", "Charlie", "David");
$names_2 = array("Alice", "Bruno");

$intersection = array_intersect($names_1, $names_2);

if(!empty($intersection) ){
    echo "The following item(s) exist in both arrays:"."<br>";
  foreach($intersection as $row){
      echo $row."<br>";
  }

}else{
echo "The arrays do not intersect";
}

Upvotes: 4

Daryl Gill
Daryl Gill

Reputation: 5524

Try:

<?php
    if (in_array($Array1, $VarToCheck)) {
        $return = true;
    }elseif (in_array($Array2, $VarToCheck)){
    $return = true;
    }
?>

Upvotes: 0

Related Questions