【问题标题】:trying to match a value in two arrays, if a matched value is present then output true尝试匹配两个数组中的值,如果存在匹配值,则输出 true
【发布时间】:2012-12-09 00:04:32
【问题描述】:

尝试匹配两个数组中的值,如果存在匹配的值,则输出 true,可能是 array_intersect 函数?很不确定!任何帮助深表感谢!

基本上有两个我不能完全放入这个框的 sql 查询!但它们每个都返回一个数组 $staffExpertise 和 $moduleExpertise,但我对 php 非常陌生,对数组相交函数更加不熟悉,所以不太确定从这里做什么!

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

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

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

    if($arrayIntersection = ){

    }

【问题讨论】:

  • 基本上有两个我不能完全放入这个盒子的 sql 查询!但他们每个人都返回一个数组 $staffExpertise 和 $moduleExpertise,但我对 php 很陌生,对数组相交函数更不熟悉,所以不太确定从这里做什么! foreach ($results as $row) { $staffExpertise = $row['expertise']; } foreach ($results as $row) { $moduleExpertise = $row['expertise']; } $arrayIntersection = array_intersect($moduleExpertise, $staffExpertise); if($arrayIntersection = ){ }
  • @FindlayMack 使用正确的格式更新您的问题。
  • 向我们展示您尝试的一些代码,否则我们无能为力。
  • 哦,编辑你的问题而不是在 cmets 中发布代码,它读起来很糟糕!
  • @FindlayMack:为什么有两个 SQL 查询?为什么不是INNER JOIN

标签: php arrays intersect array-intersect


【解决方案1】:

是的,数组相交就可以了。试试这样的:

$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";
}

【讨论】:

  • 非常感谢,这就是我所追求的那一行小代码!
【解决方案2】:

试试:

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

【讨论】:

  • 感谢您的回复,但问题是由于 $vartocheck 是从两个单独的表中获取的,所以我不能这样输入它们。例如,我正在检查讲师是否具有正确的模块专业知识来教授模块;我检查的方法是查看 moduleexpertise 表和 staffExpertise 是否具有匹配的值。如果这有任何意义?
  • 确实如此。检查 AaronSantos 的回复,我相信这可以解决问题!
  • 是的,我认为是的!为所有帮助干杯!
猜你喜欢
  • 1970-01-01
  • 2022-11-24
  • 2016-07-08
  • 2016-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-04
相关资源
最近更新 更多