dongzouya5792 2019-02-13 07:15
浏览 48
已采纳

如何使用PHP在多维数组中显示重复数据

I am using Spout Excel reader to read Excel files from php code and saving into a multidimensional array in PHP variable,Array looks like this

$array = [
[
    'id[0]' => 'BX-78',
    'Name[0]' => 'XXX',
    'Address[0]' => 'YUUSATD'
],
[
    'id[1]' => 'BX-79',
    'Name[1]' => 'YYY',
    'Address[1]' => 'DHJSHDJGY'
],
[
    'id[2]' => 'BX-80',
    'Name[2]' => 'ZZZ',
    'Address[2]' => 'DDSDSDA'
]
[
    'id[3]' => 'BX-78',
    'Name[3]' => 'AAA',
    'Address[3]' => 'FSDSDS'
][
    'id[4]' => 'BX-81',
    'Name[4]' => 'XXX',
    'Address[4]' => 'DSDSDSD'
]];

Now i want to show duplicate data from above array using two keys ['id'] and ['name'] if id repeats show as duplicate data,

  • If name repeats show that row as duplicate data if both are duplicate show as again duplicate row
  • Otherwise it is unique row.

I have tried using multidimensional array sorting but it is using only one key to match data in rows.

foreach ($arrExcelData as $v) {
   if (isset($arrExcelData[$v[0]])) {
       // found duplicate
       continue;
    }
    // remember unique item
    $arrExcelData3[$v[0]] = $v;
}

// if you need a zero-based array, otheriwse work with $_data
$arrExcelData2 = array_values($arrExcelData3);

Edited : Expected Output Result :

Matching Rows:

Id       Name    Address
-------------------------
BX-78    XXX     YUUSATD
BX-78    AAA     DDSDSDA
BX-81    XXX     DSDSDSD`
  • 写回答

4条回答 默认 最新

  • dongxin991209 2019-02-13 20:21
    关注

    If you want to list the duplicate values, I think the address of the second match should be FSDSDS as there is not item with name AAA and value DDSDSDA:

    BX-78    AAA     FSDSDS
    

    If that is the case, what you could do is to first use a double foreach to mark the arrays that contain a duplicate id or name by for example adding a property named id and name except when the array is itself in the second loop.

    After this loop, you can tell which arrays are the duplicate ones. Instead of using a corresponding index 0 as in id[0], I have used reset and next so it is not tied to these indexes.

    To get the filtered result you could use array_reduce to check for the array keys and unset them.

    For example:

    foreach ($array as $index => $a) {
        foreach ($array as $v) {
            if ($v === $a) continue;
            if (reset($v) === reset($a)) $array[$index]["id"] = "duplicate";
            if (next($v) === next($a)) $array[$index]["name"] = "duplicate";
        }
    }
    
    $array = array_reduce($array, function($carry, $item) {
        if (array_key_exists("id", $item) || array_key_exists("name", $item)) {
            unset($item["id"], $item["name"]);
            $carry[] = $item;
        }
        return $carry;
    }, []);
    
    print_r($array);
    

    Result

    Array
    (
        [0] => Array
            (
                [id[0]] => BX-78
                [Name[0]] => XXX
                [Address[0]] => YUUSATD
            )
    
        [1] => Array
            (
                [id[3]] => BX-78
                [Name[3]] => AAA
                [Address[3]] => FSDSDS
            )
    
        [2] => Array
            (
                [id[4]] => BX-81
                [Name[4]] => XXX
                [Address[4]] => DSDSDSD
            )
    
    )
    

    See a php demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档