dsm1998 2014-11-19 12:05
浏览 49
已采纳

如何在PHP中比较数组值?

I need to compare the array values with each other.These values are unique IDs. So, have to check whether ID values are repeated.

<?php
 $id=array("firstid2012","secondid2014","thirddid2010","fourthid2014");
 $idcount=count($id);
 for($i=0;$i<$idcount;$i++){
 //how to compare?? 
 }  
 ?>

If repeated id is true, then i have to change the value of that array value.So I need to know which array value is repeated also.

  • 写回答

7条回答 默认 最新

  • douyi5157 2014-11-19 12:10
    关注
    if (count($idvalues) == count(array_unique($idvalues))){
      //ALL VALUES ARE DISTINCTS
    }
    else {
      //THERE ARE DUPLICATED VALUES
      $duplicated=array();
      $visited=array();
      foreach($idvalues as $value){
          if (in_array($value,$visited)){
             $duplicated[]=$value;
          }
          $visited[]=$value;
      }
      $duplicated=array_uniq($duplicated);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度