基于共同元素合并 2 个数组

Posted

技术标签:

【中文标题】基于共同元素合并 2 个数组【英文标题】:Merge 2 arrays based on a common element 【发布时间】:2021-12-05 21:18:12 【问题描述】:

所以我有一个拉入 2 个数组的 Laravel 控制器。

数组 1:

[
    
        "id":1,
        "created_at":null,
        "updated_at":null,
        "name":"The Darkroom",
        "description":"This is the room your parents warned you about",
        "image":"https:\/\/images.unsplash.com\/photo-1579662908513-50e1433a258a?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1234&q=80",
        "is_private":"0"
    ,
        "id":2,
        "created_at":null,
        "updated_at":null,
        "name":"Smoking & Cigars",
        "description":"Time to light up and enjoy a cigar!",
        "image":"https:\/\/images.unsplash.com\/photo-1617850136763-06bc0a9a089c?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2000&q=80",
        "is_private":"0"
    ,
        "id":3,
        "created_at":null,
        "updated_at":null,
        "name":"Humiliation",
        "description":"today is the day you are going to be exposed",
        "image":"https:\/\/images.unsplash.com\/photo-1571570261702-3d23956fa32e?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2600&q=80",
        "is_private":"1"
    ,
        "id":4,
        "created_at":null,
        "updated_at":null,
        "name":"Financial Domination",
        "description":"hand over your cash and say thank you Sir!",
        "image":"https:\/\/images.unsplash.com\/photo-1526304640581-d334cdbbf45e?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2200&q=80",
        "is_private":"0"
    ,"id":5,"created_at":null,"updated_at":null,"name":"Pups & Handlers","description":"Woof, woof, bark, sit","image":"https:\/\/images.unsplash.com\/photo-1506939754500-f27bc71fccd4?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2200&q=80","is_private":"1"]

数组 2:

"channels":"presence-chat.1":"user_count":1,"presence-chat.4":"user_count":1

最终数组应如下所示:

[
   
      "id":1
      "other data in the first array"
      "user_count": 1
   
]

我需要在控制器中做的是将数据合并到一个数组中。因此,理想情况下,取第二个数组,将 chat.ID 与第一个数组的 ID 匹配,然后将 user_count 添加到其中......好吧,你明白我的意思了。

我以前从未这样做过,所以我不知道如何最好地解决这个问题。任何帮助将永远感激!

【问题讨论】:

你能告诉我们你希望最终的数组如何作为一个例子吗? 【参考方案1】:

给你,这里假设presence-chat.4 表示id 为4 的频道:

<?php

$json1 = '["id":1,"created_at":null,"updated_at":null,"name":"The Darkroom","description":"This is the room your parents warned you about","image":"https:\/\/images.unsplash.com\/photo-1579662908513-50e1433a258a?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1234&q=80","is_private":"0","id":2,"created_at":null,"updated_at":null,"name":"Smoking & Cigars","description":"Time to light up and enjoy a cigar!","image":"https:\/\/images.unsplash.com\/photo-1617850136763-06bc0a9a089c?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2000&q=80","is_private":"0","id":3,"created_at":null,"updated_at":null,"name":"Humiliation","description":"today is the day you are going to be exposed","image":"https:\/\/images.unsplash.com\/photo-1571570261702-3d23956fa32e?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2600&q=80","is_private":"1","id":4,"created_at":null,"updated_at":null,"name":"Financial Domination","description":"hand over your cash and say thank you Sir!","image":"https:\/\/images.unsplash.com\/photo-1526304640581-d334cdbbf45e?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2200&q=80","is_private":"0","id":5,"created_at":null,"updated_at":null,"name":"Pups & Handlers","description":"Woof, woof, bark, sit","image":"https:\/\/images.unsplash.com\/photo-1506939754500-f27bc71fccd4?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2200&q=80","is_private":"1"]';
$json2 = '"channels":"presence-chat.1":"user_count":1,"presence-chat.4":"user_count":1';

// convert the json into php arrays
$array1 = json_decode($json1, true);
$array2 = json_decode($json2, true);

// we extract the user_counts from the second array
// and make the index of a new array the channel id with value user_count
$channel_counts = [];
foreach ($array2['channels'] as $chan_name => $count) 
    $channel_id = explode('.', $chan_name)[1];
    $user_count = $count['user_count'];
    $channel_counts[$channel_id] = $user_count;


// we pass this array by reference as we are modifying it
foreach ($array1 as &$channel) 
    $id = $channel['id'];
    if (isset($channel_counts[$id]))
        $channel['user_count'] = $channel_counts[$id];
    else
        $channel['user_count'] = 0;

unset($channel);

$final_json = json_encode($array1, JSON_PRETTY_PRINT);

echo $final_json;

结果:

[
    
        "id": 1,
        "created_at": null,
        "updated_at": null,
        "name": "The Darkroom",
        "description": "This is the room your parents warned you about",
        "image": "https:\/\/images.unsplash.com\/photo-1579662908513-50e1433a258a?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1234&q=80",
        "is_private": "0",
        "user_count": 1
    ,
    
        "id": 2,
        "created_at": null,
        "updated_at": null,
        "name": "Smoking & Cigars",
        "description": "Time to light up and enjoy a cigar!",
        "image": "https:\/\/images.unsplash.com\/photo-1617850136763-06bc0a9a089c?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2000&q=80",
        "is_private": "0",
        "user_count": 0
    ,
    
        "id": 3,
        "created_at": null,
        "updated_at": null,
        "name": "Humiliation",
        "description": "today is the day you are going to be exposed",
        "image": "https:\/\/images.unsplash.com\/photo-1571570261702-3d23956fa32e?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2600&q=80",
        "is_private": "1",
        "user_count": 0
    ,
    
        "id": 4,
        "created_at": null,
        "updated_at": null,
        "name": "Financial Domination",
        "description": "hand over your cash and say thank you Sir!",
        "image": "https:\/\/images.unsplash.com\/photo-1526304640581-d334cdbbf45e?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=2200&q=80",
        "is_private": "0",
        "user_count": 1
    , ...snip
]

【讨论】:

所以这和你给出的代码一样工作得很好,但是当我用我用来抓取数组的代码替换 json2 时,它给了我错误 "json_decode(): Argument #1 ($json ) 必须是字符串类型,stdClass given" 但是如果我返回数组,它就会显示,就像我将它粘贴到我的问题中一样。所以是的,你有正确的答案我只是不知道为什么它不适合我 你是如何获得 JSON 数组的? 在数组失败的情况下......它正在使用推送器 HTTP API - $pusherUserCount = $pusher->getChannels(['filter_by_prefix' => 'presence-', 'info' => 'user_count']) - 正如我所说 - 如果我返回 $pusherUserCount 它会显示数组,就像你在上面使用它一样 好的,试试$array2 = $pusher-&gt;getChannels(&lt;snip&gt;)-&gt;channels,去掉json_decode 不,仍然没有这样做。我要乱搞,看看是什么原因造成的。就像我说的那样,我已经尝试过使用您发布的静态数组并且它有效,我已经获取了数组并将其在线转换为 PHP 数组并且有效。所以我不确定这里有什么区别。

以上是关于基于共同元素合并 2 个数组的主要内容,如果未能解决你的问题,请参考以下文章

1.合并两个数组,并保持仍然有序。2.删除合并后数组中的重复元素

JAVA,用List做,两个数组中数的合并和去除相同元素

二路归并排序(也叫合并排序)

数组的操作

华为OJ平台——整形数组合并

将两个列数组合并为配置单元中的 1 个数组列