php - 如何从父数组中删除重复值或特定值
问题描述
我有 2 个数组,我只想从父数组中删除那些在嵌套数组或子数组中相同的值。
以下是我的数组输出。
{
"status": true,
"data": [
{
"comment_id": "873",
"user_id": "1",
"item_id": "833",
"comment_details": "demo220"
},
{
"comment_id": "876",
"user_id": "1",
"item_id": "833",
"comment_details": "TEEDSDS"
},
{
"comment_id": "872",
"user_id": "1",
"item_id": "833",
"comment_details": "Test"
},
{
"comment_id": "880",
"user_id": "1",
"item_id": "833",
"comment_details": "Amazing POst"
},
{
"comment_id": "881",
"user_id": "1",
"item_id": "833",
"comment_details": "MY first conntetn"
},
{
"comment_id": "881",
"user_id": "1",
"item_id": "833",
"comment_details": "MY first conntetn",
"post_id": "833",
"comment_replay": {
"comment_id": "882",
"user_id": "168",
"comment_details": "Thank you"
}
},
{
"comment_id": "880",
"user_id": "1",
"item_id": "833",
"comment_details": "Amazing POst",
"post_id": "833",
"comment_replay": {
"comment_id": "883",
"user_id": "1",
"comment_details": "test"
}
},
{
"comment_id": "872",
"user_id": "1",
"item_id": "833",
"comment_details": "Test",
"post_id": "833",
"comment_replay": {
"comment_id": "884",
"user_id": "168",
"comment_details": "Hello 2020"
}
}
]
}
在这里你可以在两个数组中看到 idcomment_id
880,881,872
我只想从父数组中删除这 3 个 ide。
我的脚本是..
$listComment = array();
$commentData = array();
foreach ($commentQuery as $key => $value) {
$listComment['comment_id'] = $value->id;
$listComment['user_id'] = $value->user_id;
$listComment['item_id'] = $value->item_id;
$listComment['comment_details'] = $value->content;
if($value->item_id != $value->secondary_item_id){
if (($key = array_search($value->secondary_item_id, $commentQuery)) !== false) {
unset($commentQuery[$key]);
}
$listReplay =$wpdb->get_results("(SELECT * FROM crisss_bp_activity WHERE id = $value->secondary_item_id AND type ='activity_comment') UNION DISTINCT
(SELECT * FROM crisss_bp_activity WHERE secondary_item_id = $value->secondary_item_id AND type ='activity_comment')");
$found_mainkit = false;
foreach ($listReplay as $key => $val) {
if (($key = array_search($val->id, $commentQuery)) !== false) {
unset($commentQuery[$key]);
}
if($val->item_id == $val->secondary_item_id){
$listComment['post_id'] =$val->item_id;
$listComment['comment_id'] =$val->id;
$listComment['user_id'] = $val->user_id;
$listComment['comment_details'] = $val->content;
}else{
$listComment['comment_replay']['comment_id'] =$val->id;
$listComment['comment_replay']['user_id'] =$val->user_id;
$listComment['comment_replay']['comment_details'] =$val->content;
}
}
}
$commentData[] = $listComment;
}
$result = array('status'=>true,'message'=>'Comment added successfully.','data'=>$commentData);
父数组是$commentQuery
And$listReplay
是一个嵌套数组。我只想从不在嵌套的父数组中删除或取消设置重复值。
我想要这样的输出...
{
"status": true,
"data": [
{
"comment_id": "873",
"user_id": "1",
"item_id": "833",
"comment_details": "demo220"
},
{
"comment_id": "876",
"user_id": "1",
"item_id": "833",
"comment_details": "TEEDSDS"
},
{
"comment_id": "881",
"user_id": "1",
"item_id": "833",
"comment_details": "MY first conntetn",
"post_id": "833",
"comment_replay": {
"comment_id": "882",
"user_id": "168",
"comment_details": "Thank you"
}
},
{
"comment_id": "880",
"user_id": "1",
"item_id": "833",
"comment_details": "Amazing POst",
"post_id": "833",
"comment_replay": {
"comment_id": "883",
"user_id": "1",
"comment_details": "test"
}
},
{
"comment_id": "872",
"user_id": "1",
"item_id": "833",
"comment_details": "Test",
"post_id": "833",
"comment_replay": {
"comment_id": "884",
"user_id": "168",
"comment_details": "Hello 2020"
}
}
]
}
解决方案
继续脚本的结果,其中包含以下数组:
$res = array (
'status' => true,
'data' =>
array (
0 =>
array (
'comment_id' => '873',
'user_id' => '1',
'item_id' => '833',
'comment_details' => 'demo220'
),
1 =>
array (
'comment_id' => '876',
'user_id' => '1',
'item_id' => '833',
'comment_details' => 'TEEDSDS'
),
2 =>
array (
'comment_id' => '872',
'user_id' => '1',
'item_id' => '833',
'comment_details' => 'Test'
),
3 =>
array (
'comment_id' => '880',
'user_id' => '1',
'item_id' => '833',
'comment_details' => 'Amazing POst'
),
4 =>
array (
'comment_id' => '881',
'user_id' => '1',
'item_id' => '833',
'comment_details' => 'MY first conntetn'
),
5 =>
array (
'comment_id' => '881',
'user_id' => '1',
'item_id' => '833',
'comment_details' => 'MY first conntetn',
'post_id' => '833',
'comment_replay' =>
array (
'comment_id' => '882',
'user_id' => '168',
'comment_details' => 'Thank you'
)
),
6 =>
array (
'comment_id' => '880',
'user_id' => '1',
'item_id' => '833',
'comment_details' => 'Amazing POst',
'post_id' => '833',
'comment_replay' =>
array (
'comment_id' => '883',
'user_id' => '1',
'comment_details' => 'test'
)
),
7 =>
array (
'comment_id' => '872',
'user_id' => '1',
'item_id' => '833',
'comment_details' => 'Test',
'post_id' => '833',
'comment_replay' =>
array (
'comment_id' => '884',
'user_id' => '168',
'comment_details' => 'Hello 2020'
)
)
)
);
您可以通过以下方式从父数组中删除任何重复的注释:
$data = $res['data'];
$dups = [];
$commentsWithCommentReplays = [];
foreach($data as $key => $elem){
if(isset($elem['comment_replay'])){
$commentsWithCommentReplays[] = $elem['comment_id'];
}
}
foreach($data as $key => $elem){
if(in_array($elem['comment_id'], $commentsWithCommentReplays) && !isset($elem['comment_replay'])){
unset($data[$key]);
}
}
这将返回以下数组:
Array
(
[0] => Array
(
[comment_id] => 873
[user_id] => 1
[item_id] => 833
[comment_details] => demo220
)
[1] => Array
(
[comment_id] => 876
[user_id] => 1
[item_id] => 833
[comment_details] => TEEDSDS
)
[5] => Array
(
[comment_id] => 881
[user_id] => 1
[item_id] => 833
[comment_details] => MY first conntetn
[post_id] => 833
[comment_replay] => Array
(
[comment_id] => 882
[user_id] => 168
[comment_details] => Thank you
)
)
[6] => Array
(
[comment_id] => 880
[user_id] => 1
[item_id] => 833
[comment_details] => Amazing POst
[post_id] => 833
[comment_replay] => Array
(
[comment_id] => 883
[user_id] => 1
[comment_details] => test
)
)
[7] => Array
(
[comment_id] => 872
[user_id] => 1
[item_id] => 833
[comment_details] => Test
[post_id] => 833
[comment_replay] => Array
(
[comment_id] => 884
[user_id] => 168
[comment_details] => Hello 2020
)
)
)
推荐阅读
- javascript - Node.js - 如何使用 package.json 运行多个服务器
- javascript - 流服务器意外重启
- swiftui - Quandry 文本存储
- android-studio - Android studio kotlin,如何获取随机图像数组?
- javascript - 我需要做什么才能使用 createMediaElementSourceNode 播放音乐?
- google-cloud-composer - Composer/Airflow - 在运行 DAG/管道时添加依赖项?
- java - 在从 Intellij IDEA 创建的 jar 文件中播放 .wav 时出现问题
- python - 如何将布尔数组转换为矩阵?
- reactjs - 从 switch material-ui 返回是或否
- graphics - glTF:我们如何存储基本几何图元?