如何从父数组中删除重复值或特定值

时间:2020-04-21 18:29:28

标签: php arrays wordpress

我有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"
            }
        }
    ]
}

在这里您可以在两个数组中看到id comment_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,而$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"
            }
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

从脚本的结果继续,其中包含以下数组:

$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
                )

        )

)
相关问题