根据一个公共元素合并两个数组



所以我有一个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}}}

Final Array应该是这样的:

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

我需要在我的控制器中做的是将数据合并到一个数组中。理想情况下,取第二个数组,匹配聊天。ID与第一个数组的ID,并添加user_count到它…你懂我的意思吧。

我从来没有这样做过,所以我不知道如何最好地去做这件事。任何帮助将永远感激!

好了,这里假设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
]

相关内容

  • 没有找到相关文章

最新更新