dsk95913 2014-12-07 19:56
浏览 6

检查两个以上的阵列是否具有相同的数据

Let's say all these all have the same data in common.

How would I do this in this multidimensional array.

I'm basically trying to find all the same elements using

array_intersect($array,$array);

but I cannot figure out what I should be doing in order to do that.

array(14) {
  [0]=>
  string(5) "0.1"
    [1]=>
    string(5) "0.2"
    [2]=>
    string(5) "0.3"
    [3]=>
    string(5) "0.4"
    [4]=>
    string(5) "0.1"
    [5]=>
    string(5) "0.2"
    [6]=>
    string(5) "0.3"
    [7]=>
    string(5) "0.4"
    [8]=>
    string(5) "0.2"
    [9]=>
    string(5) "0.3"
    [10]=>
    string(5) "0.4"
    [11]=>
    string(5) "0.1"
    [12]=>
    string(5) "0.2"
    [13]=>
    string(5) "0.3"
  }
  • 写回答

1条回答 默认 最新

  • dtwvr26066 2014-12-07 20:38
    关注

    Not sure what you are exactly looking for:

    $i=0;  // index of the orginal array
    $common = array_reduce($array, function ($c, $v) use (&$i) {
        foreach ($v['data'] as $nb) {
            $c[$nb][] = $i; // store the index for the current number
        }
        $i++; // next index
        return $c;
    }, array());
    
    // remove the unique values
    $common = array_filter($common, function($item) { return count($item)>1; });
    
    // sort by decreasing number of indexes
    uasort($common, function ($a, $b) {return count($b) - count($a);});
    
    print_r($common);
    

    This will produce an associative array with the common number as key, and an array with the indexes of the original array as value:

    Array
    (
        [5] => Array (
                [0] => 0
                [1] => 1
                [2] => 2
                [3] => 3
            )
        [4] => Array (
                [0] => 0
                [1] => 1
                [2] => 3
            )
        [2] => Array (
                [0] => 0
                [1] => 1
                [2] => 3
            )
        [6] => Array (
                [0] => 1
                [1] => 3
            )
        [1] => Array (
                [0] => 1
                [1] => 3
            )
        [3] => Array (
                [0] => 1
                [1] => 3
            )
    )
    
    评论

报告相同问题?

悬赏问题

  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection