douxu3315 2016-05-04 17:28
浏览 24
已采纳

在多维数组中搜索重复项,并返回重复值和数组键

I have the following multidimensional array:

$ancestors = array(
[1] =>array("Andrew","Adele"), 
[2] =>array("Bertha","Bill","Billy","Blake"), 
[3] =>array("Cathy","Case","Cory","Creek","Andrew","Ceaser","Cece","Childer"));

I am trying to search the rows and return duplicate values along with the array key of the lowest number the duplicate appears and return values to an associative array:

$common = array("Andrew"=>"1");
  • 写回答

3条回答 默认 最新

  • doupai6875 2016-05-04 17:49
    关注
    // merge all array in one
    $new = array();
    foreach($ancestors as $i)
       $new = array_merge($new, $i);
    
    // make array value => count and sort in in descendant order
    $ar = array_count_values($new);
    arsort($ar);
    
    // save result array while count > 1
    $result = array();
    foreach($ar as $k => $v) {
      if ($v < 2) break;
      $result[$k] = array_search($k, $new) + 1;
    }
    
    print_r($result);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法