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条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题