doutuosai3504 2016-06-22 09:59
浏览 62
已采纳

数组唯一并合并对象数组中的值

I have one big array and I want to make it unique on doc_id and merge roleid

 Array
(
    [0] => stdClass Object
        (
            [title] => Test ÆØÅ
            [doc_id] => 279
            [mid] => 
            [type] => 3
            [label] => 
            [roleid] =>  76, 276
        )

    [1] => stdClass Object
        (
            [title] => Test ÆØÅ
            [doc_id] => 279
            [mid] => 
            [type] => 3
            [label] => 
            [roleid] =>  76, 276
        )

    [2] => stdClass Object
        (
            [title] => NVD AS
            [doc_id] => 415
            [mid] => 
            [type] => 3
            [label] => 
            [roleid] =>  2
        )

    [3] => stdClass Object
        (
            [title] => NVD AS
            [doc_id] => 415
            [mid] => 
            [type] => 3
            [label] => 
            [roleid] =>  76, 276
        )

    [4] => stdClass Object
        (
            [title] => Test for SN
            [doc_id] => 506
            [mid] => 
            [type] => 3
            [label] => 
            [roleid] =>  76, 276
        )

    [5] => stdClass Object
        (
            [title] => Test for SN
            [doc_id] => 506
            [mid] => 
            [type] => 3
            [label] => 
            [roleid] =>  6
        )

Just to make it unique I am using below code

foreach ($arry as $val) {
            $newArr[$arry ->doc_id] = $val;
        }
        $result = array_values($newArr);

I am able to get unique document but I lost roleid, I need to merge roleid with doc_id it should be

Array
(
    [0] => stdClass Object
        (
            [title] => Test ÆØÅ
            [doc_id] => 279
            [mid] => 
            [type] => 3
            [label] => 
            [roleid] =>  76, 276
        )

    [1] => stdClass Object
        (
            [title] => NVD AS
            [doc_id] => 415
            [mid] => 
            [type] => 3
            [label] => 
            [roleid] =>  2,76, 276
        )

    [2] => stdClass Object
        (
            [title] => Test for SN
            [doc_id] => 506
            [mid] => 
            [type] => 3
            [label] => 
            [roleid] => 6,76, 276
        )
Can anyone help in this

Thanks in advance

  • 写回答

2条回答 默认 最新

  • druhoytza979667566 2016-06-22 10:18
    关注

    Your current format of roleid is not in the best format for processing. If you change it to array you can do something similar to this:

    $item1 = new stdClass();
    $item1->doc_id = 279;
    $item1->role_id = '76, 276, 3';
    
    $item2 = new stdClass();
    $item2->doc_id = 279;
    $item2->role_id = '1, 44, 76';
    
    $item3 = new stdClass();
    $item3->doc_id = 279;
    $item3->role_id = '8, 44, 1';
    
    $data = [$item1, $item2, $item3];
    
    $result = [];
    foreach ($data as $item) {
        if (isset($result[$item->doc_id])) {
            $existingIds = explode(', ', $result[$item->doc_id]->role_id);
            $newIds = explode(', ', $item->role_id);
    
            $result[$item->doc_id]->role_id = implode(', ', array_unique(array_merge(
                $existingIds,
                $newIds
            )));
    
            continue;
        }
    
        $result[$item->doc_id] = $item;
    }
    
    
    print_r($result);
    

    With the result:

    Array
    (
        [279] => stdClass Object
            (
                [doc_id] => 279
                [role_id] => 76, 276, 3, 1, 44, 8
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱