如何根据多个值查找重复数组?

时间:2016-08-02 13:04:53

标签: php arrays

我正在编写一个脚本,只获取字段值相同的数组。请检查下面的代码。我是PHP开发的新手,所以请帮助我。     代码:

foreach($leads_details->result as  $data){

  array_push($matchFullName,$data->fullName);
  array_push($matchEmailName,$data->email);
  array_push($matchCompanyName,$data->company);

  if ( in_array($data->fullName, $matchFullName) && in_array($data->email, $matchEmailName) && in_array($data->company, $matchCompanyName) ) {  

    echo 'Same Full Name + Same Email ID + Same Company Name ';
    array_push($samefullname_sameemailid_samecompany,$data2);

  }

  if ( in_array($data->fullName, $matchFullName) && in_array($data->email, $matchEmailName) && ( !in_array($data->company, $matchCompanyName) ) )  {    

    echo 'Same Full Name + Same Email ID + Different Company Name  ';
    array_push($samefullname_sametemailid_differentcompany,$data2);

  } 
} 

我得到的结果:

Array
(
    [0] => stdClass Object
        (
            [id] => 21748
            [updatedAt] => 2016-07-27T12:05:56Z
            [lastName] => Rennert           
            [fullName] => Angela Rennert
            [email] => angela.rennert@chronus.com           
            [createdAt] => 2016-05-05T09:59:37Z
            [company] => Chronus
            [firstName] => Angela
        )

    [1] => stdClass Object
        (
            [id] => 1173134
            [updatedAt] => 2016-07-27T12:07:52Z
            [lastName] => Rennert            
            [fullName] => Angela Rennert
            [email] => angela.rennert@chronus.com          
            [createdAt] => 2016-03-01T10:22:57Z
            [company] => Chronus - Mentoring and Talent Development Solutions
            [firstName] => Angela
        )

    [1] => stdClass Object
        (
            [id] => 1173134
            [updatedAt] => 2016-07-27T12:07:52Z
            [lastName] => Rennert           
            [fullName] => Angela Rennert
            [email] => angela.rennert@chronus.com           
            [createdAt] => 2016-03-01T10:22:57Z
            [company] => Chronus - Mentoring and Talent Development Solutions
            [firstName] => Angela
        )   

)

现在我必须找到fullnameemailcompany名称相同的数组值。

所以结果应该是这样的: -

Array
(
    [1] => stdClass Object
        (
            [id] => 1173134
            [updatedAt] => 2016-07-27T12:07:52Z
            [lastName] => Rennert            
            [fullName] => Angela Rennert
            [email] => angela.rennert@chronus.com          
            [createdAt] => 2016-03-01T10:22:57Z
            [company] => Chronus - Mentoring and Talent Development Solutions
            [firstName] => Angela
        )

    [1] => stdClass Object
        (
            [id] => 1173134
            [updatedAt] => 2016-07-27T12:07:52Z
            [lastName] => Rennert           
            [fullName] => Angela Rennert
            [email] => angela.rennert@chronus.com           
            [createdAt] => 2016-03-01T10:22:57Z
            [company] => Chronus - Mentoring and Talent Development Solutions
            [firstName] => Angela
        )   

)

2 个答案:

答案 0 :(得分:0)

我认为这对您有用,请查看评论以获得解释 -

$tempArray = $newArr = array();

foreach ($your_array as $val) {
  // implode the current duplicate values in $temp array
  $implodedVal = "{$val['lastName']}, {$val['fullName']}, {$val['company']}";
  // if they are not present in temp array means they are unique, 
  // so push it in $newArr
  if (!in_array($implodedVal, $tempArray)) {
    $newArr[] = $val;
  }
  $tempArray[] = $implodedVal;
}


var_dump($newArr); //this should be the required output

答案 1 :(得分:0)

您好我已经分享了示例脚本,请在下面查看。

$arr = array(1,2,3,4,4,5,8,9,3);
$a = $b = array();

function w($k,$v)
{
    global $a;
    global $b;

    if(!in_array($k,$a))
    {
        $a[] = $k;
    }
    else
    {
        $b[] = $k;
    }
}

array_walk($arr,"w");

print_r($a);
echo "\n";
print_r($b);