douguang9014 2017-02-24 05:55
浏览 41
已采纳

PHP PDO通过in_array()方法比较数组元素是行不通的

I need to compare element from an array to another array with in_array() method:

$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()
}

Now i want to compare the above array elements with each element in the iterated loop over the following loop

<?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()
    }
?>

in fact it populate the following array:

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 ) 
)

but i have no luck to compare, because the array contained with another array and i don't know what to do with this now ?

  • 写回答

1条回答 默认 最新

  • doukuanyong1939 2017-02-24 07:45
    关注

    You need to loop through the parent array to compare to the inner arrays.

    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
        }
    }
    

    This way you'll be able to check wether whatever you're looking for is inside one of the results you got from the database

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历