基于多个键从多维数组中删除重复项

时间:2017-03-03 06:27:38

标签: php arrays multidimensional-array

很抱歉,如果之前有人问这个问题,但我搜索了很多内容并且无法找到解决方案。 我一直试图解决这个问题一段时间,并且无法为它编写函数。

我有一个这样的数组:

req

我需要编写一个函数,根据多个键删除此数组的重复项,因此我的函数调用看起来应该是这样的:

{ stripeToken: 'tok_19t9jcLhf04QCVXXXXXXXX',
 stripeTokenType: 'card',
 stripeEmail: 'me@gmail.com',
 stripeBillingName: 'Clark',
 stripeBillingAddressCountry: 'United States',
 stripeBillingAddressCountryCode: 'US',
 stripeBillingAddressZip: 'Zip',
 stripeBillingAddressLine1: 'My address',
 stripeBillingAddressCity: 'City',
 stripeBillingAddressState: 'State' }

换句话说,一个号码不能多次出现在同一个群组中。

按数组调用unique_by_keys()之后应该是这样的:

    $numbers = array(
        array("tag" => "developer", "group" => "grp_1", "num" => "123123"),
        array("tag" => "developer", "group" => "grp_2", "num" => "111111"),
        array("tag" => "student", "group" => "grp_1", "num" => "123123"),
        array("tag" => "student", "group" => "grp_2", "num" => "123123"),
        array("tag" => "developer", "group" => "grp_3", "num" => "111111"),
    );

如果您能帮我找到解决方案,或者引导我采用正确的思维方式,我会感激不尽。 谢谢!

解: 通过编写以下函数,我找到了一个解决方案: (我以接受多种形式的$ haystack数组的方式编写它)

unique_by_keys($numbers, array("num","group"));

感谢所有回复的人!

3 个答案:

答案 0 :(得分:3)

Bhaskar的方法在循环中分配唯一键来删除重复项,为这种情况提供了一个非常小的功能。

以下是一个以前不必要的复杂版本:

function unique_by_keys($haystack=array(),$needles=array()){
    // reverse order of sub-arrays to preserve lower-indexed values
    foreach(array_reverse($haystack) as $row){
        $result[implode('',array_intersect_key($row,array_flip($needles)))]=$row; // assign unique keys
    }
    ksort($result);  // sort the sub-arrays by their assoc. keys
    return array_values($result); // replace assoc keys with indexed keys
}

这是我能提出的最佳/最精简的解决方案:

$numbers = array(
        array("tag" => "developer", "group" => "grp_1", "num" => "123123"),
        array("tag" => "developer", "group" => "grp_2", "num" => "111111"),
        array("tag" => "student",   "group" => "grp_1", "num" => "123123"),
        array("tag" => "student",   "group" => "grp_2", "num" => "123123"),
        array("tag" => "developer", "group" => "grp_3", "num" => "111111")
    );

function unique_by_keys($haystack=array(),$needles=array()){
    foreach($haystack as $row){
        $key=implode('',array_intersect_key($row,array_flip($needles)));  // declare unique key
        if(!isset($result[$key])){$result[$key]=$row;} // save row if non-duplicate
    }
    return array_values($result);
}

echo "<pre>";
var_export(unique_by_keys($numbers,array("group","num")));
echo "</pre>";

输出:

array (
  0 => 
  array (
    'tag' => 'developer',
    'group' => 'grp_1',
    'num' => '123123',
  ),
  1 => 
  array (
    'tag' => 'developer',
    'group' => 'grp_2',
    'num' => '111111',
  ),
  2 => 
  array (
    'tag' => 'student',
    'group' => 'grp_2',
    'num' => '123123',
  ),
  3 => 
  array (
    'tag' => 'developer',
    'group' => 'grp_3',
    'num' => '111111',
  ),
)

答案 1 :(得分:1)

$newNumbers = array();
foreach($numbers as $key=>$values){
    $newkey = $values['group'].'__'.$values['num']; 
    $newNumbers[$newkey] = $values;
}
var_dump($newNumbers)

答案 2 :(得分:0)

代码可能效率不高,但我会为你工作:)

$result = unique_by_keys($numbers, array("num","group"));

echo "<pre>";
print_R($result);

function unique_by_keys($numbers, $arr){
    $new_array = array();
    $output = array();
    foreach ($numbers as $n){

        if(isset($new_array[$n[$arr[1]]]) && $new_array[$n[$arr[1]]] == $n[$arr[0]]){
            continue;
        }else{
            $new_array[$n[$arr[1]]] = $n[$arr[0]];
            $output[] = $n;
        }

    }
    return $output;
}