Home > front end >  Merge 2 arrays based on a common element
Merge 2 arrays based on a common element

Time:10-19

So I have a Laravel controller that is pulling in 2 arrays.

Array 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"}]

Array 2:

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

Final Array should look like this:

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

What I NEED to do inside my controller is merge the data together into a single array. So ideally, take the second array, match up the chat.ID with the ID of the first array and add in user_count to it... well you get what I mean.

I Have NEVER done this before so I have NO IDEA how best to go about this. Any help would be forever appreciated!

CodePudding user response:

Here you go, this assumes that presence-chat.4 means channel with the id of 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;

Which results in:

[
    {
        "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
]
  • Related