douwendu2460 2016-11-12 10:55
浏览 65

通过使用PHP比较两个数组,无法正确显示回显值

I need to display some echo value by comparing two array using PHP. I am explaining my code below.

$maindata=array(array('id'=>3),array('id'=>7),array('id'=>9));
 $childata=array(array('id'=>3),array('id'=>45),array('id'=>7),array('id'=>123));
    for($i=0;$i < count($childata);$i++){
        if(in_array($childata[$i],$maindata)){
            echo "get the value 
".$maindata[$i]['id'];
            echo "delete the value 
".$maindata[$i]['id'];
            echo "insert the value 
".$maindata[$i]['id'];
        }
        if(!in_array($childata[$i],$maindata)){
             echo "delete the value 
".$childata[$i]['id'];
        }
    }  

Here my requirement is if any data from first array($maindata) is present in second array(i.e-$childata) then the first three echo message will display and other value from second array(i.e-$childata) the second only one echo message will display. But in some cases it is not working like below.

Cases:

$maindata=array(array('id'=>3),array('id'=>7),array('id'=>9));
 $childata=array(array('id'=>3),array('id'=>45),array('id'=>123));


               or

$maindata=array(array('id'=>3),array('id'=>7),array('id'=>9));
 $childata=array(array('id'=>3),array('id'=>45));

The first case from above I need for all value from $maindata array the first three echo message will display and for array('id'=>45) and array('id'=>123) the only last one echo message will display. Similarly for the second case from above all value from $maindata array the first three echo message will display and for array('id'=>45) the only last one echo message will display.

  • 写回答

2条回答 默认 最新

  • dongtao5104 2016-11-12 11:17
    关注

    The test if (count($maindata) < count($childata)) { is false for your 2 last examples.

    I suggest you remove this test.

    评论

报告相同问题?

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”