PHP remove array of associative array that have a duplicate values of specific key

前端 未结 3 1076
鱼传尺愫
鱼传尺愫 2021-01-29 13:11

I\'ve a array of associative array

array(xxx) {
 [0]=>
   array(3) {
    [\"group_id\"]=>2
    [\"contact\"]=> \"foo\"
    [\"contact_email\"]=> \"fo         


        
相关标签:
3条回答
  • 2021-01-29 13:34

    You can use foreach and group them by contact_email

    $r = [];
    foreach($a as $v){
     $r[$v['contact_email']] = $v;
    }
    print_r(array_values($r));// reorder index
    

    Working example : https://3v4l.org/0oN8h

    0 讨论(0)
  • 2021-01-29 13:48

    Extract to an array and index by contact_email. Since there cannot be duplicate indexes you'll get the last occurrence:

    $array = array_column($array, null, 'contact_email');
    

    If you want to re-index that back to integers:

    $array = array_values(array_column($array, null, 'contact_email'));
    
    0 讨论(0)
  • 2021-01-29 13:48

    I think this can help

    $arr = [['contact_email' => 'a@a.com'], ['contact_email' => 'a@a.com'], ['contact_email' => 'b@a.com']];
    $result = [];
    array_map(function ($item) use (&$result) {
        $result[$item['contact_email']] = $item;
    }, $arr);
    print_r($result);
    
    0 讨论(0)
提交回复
热议问题