PHP PDO 通过 in_array() 方法比较数组元素不起作用

标签 php mysql arrays compare

我需要使用 in_array() 方法将一个数组中的元素与另一个数组进行比较:

$oldAttachedObjs = array();//this array need to be compare with single element of other loop     

$sqlObjAttached = "SELECT sno,ins_sno,obj_sno FROM cases_objections WHERE ins_sno = :ins_sno";
$paramObj = array(':ins_sno'=>$ins_sno);

if($db->dbQuery($sqlObjAttached,$paramObj)){
    foreach($db->getRecordSet($sqlObjAttached,$paramObj) as $o){
        $oldAttachedObjs[] = $o;
    }//foreach()
}

现在我想将上面的数组元素与以下循环中的迭代循环中的每个元素进行比较

<?php 
    $sqlChildren = "SELECT sno,obj_sno,child_lbl FROM list_objection_children WHERE is_active = 1 AND obj_sno = :obj_sno ORDER BY sno ASC";                                    
    $param = array(':obj_sno'=>$obj['sno']);                               

    if($db->dbQuery($sqlChildren,$param)){
        foreach($db->getRecordSet($sqlChildren,$param) as $ch){
?>

<ul class="obj_ul">
    <li>
         <div class="checkbox checkbox-primary">
         <?php print_r($oldAttachedObjs[0]); ?>
             <input type="checkbox" <?php if(in_array($ch['sno'],$oldAttachedObjs['obj_sno'])){ ?> checked <?php //} ?> value="1" id="chk_<?php echo($ch['sno']);?>" name="chk_<?php echo($ch['sno']);?>" class="styled">
             <label style="font-weight: normal !important;" for="chk_<?php echo($ch['sno']);?>"><?php echo($ch['child_lbl']); ?></label>
         </div>
    </li>
</ul>

<?php
        }//foreach()
    }
?>

事实上它填充了以下数组:

Array ( 
    [0] => Array ( [sno] => 1 [0] => 1 [ins_sno] => 2 [1] => 2 [obj_sno] => 3 [2] => 3 ),
    [1] => Array ( [sno] => 2 [0] => 2 [ins_sno] => 2 [1] => 2 [obj_sno] => 49 [2] => 49 ), 
    [2] => Array ( [sno] => 3 [0] => 3 [ins_sno] => 2 [1] => 2 [obj_sno] => 52 [2] => 52 ),
    [3] => Array ( [sno] => 5 [0] => 5 [ins_sno] => 2 [1] => 2 [obj_sno] => 54 [2] => 54 ) 
)

但我没有运气进行比较,因为该数组包含另一个数组,而我现在不知道如何处理它?<​​/p>

最佳答案

您需要循环访问父数组以与内部数组进行比较。

foreach($oldAttachedObjs as $objs){
    if(in_array('what you are looking for', $objs){
        //it is in the array, do what you want
    }else{
        //it is not in the array, deal with it accordingly
    }
}

这样您就可以检查您要查找的内容是否包含在从数据库获得的结果之一中

关于PHP PDO 通过 in_array() 方法比较数组元素不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42431769/

相关文章:

php - 如何使用不同的where子句将2个mysql查询合并为1个

java - 用户 'root' @'localhost' 的访问被拒绝(使用密码 : YES )

mysql - 如何在不使用子查询的情况下修复此 mysql 查询?

php - 提高 php-cli 中大型 csv 文件解析的性能

Java:从另一个数组创建数组

javascript - 数组修改

php - 为什么这个 PHP 无法验证?

找不到 Php 类 'DateInterval'

php - Mysql:一般错误不正确的整数值

java - IndexOutOfBoundException 使用 java.util.Vector