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 QTableWidget重绘程序崩溃
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding