如何删除不具有重复键的数组中的任何记录?

时间:2016-10-27 11:01:45

标签: php arrays

<?php

$temp = [];
$array = [];

$array[] = array("parent_id" => 1, "parent" => "Mr & Mrs Lacey", "child_firstname" => "callum", "child_lastname" => "lacey");
$array[] = array("parent_id" => 2, "parent" => "Mr and Mrs Lacey", "child_firstname" => "daniel", "child_lastname" => "lacey");
$array[] = array("parent_id" => 3, "parent" => "Mr and Mrs Lacey", "child_firstname" => "sam", "child_lastname" => "lacey");
$array[] = array("parent_id" => 4, "parent" => "Mr and Mrs Dunn", "child_firstname" => "daniel", "child_lastname" => "dunn");
$array[] = array("parent_id" => 5, "parent" => "Mr and Mrs Parker", "child_firstname" => "sam", "child_lastname" => "parker");


function stripString($input){
    $input = preg_replace("/[^a-zA-Z]+/", "", $input);
    return $input;
}

foreach($array as $item){
    $input = str_replace(" and ","", $item["parent"]);
    $parent = stripString($input);
    $child_firstname = stripString($item["child_firstname"]);
    $child_lastname = stripString($item["child_lastname"]);

    if(!array_key_exists($parent, $temp)) {  //Add only first index details to array
        $temp[$parent]['parent_id'] = $item["parent_id"];
        $temp[$parent]['parent'] = $parent;
    } else {
        $temp[$parent]['duplicates'][] = $item["parent_id"];
    }

    // Remove unwanted indices
    $temp[$parent][] = array("child_firstname" => $child_firstname, 
        "child_lastname" => $child_lastname);        

}

$temp = array_values($temp);  // Reset index

print_r($temp);

打印出结果:

Array
(
    [0] => Array
        (
            [parent_id] => 1
            [parent] => MrMrsLacey
            [0] => Array
                (
                    [child_firstname] => callum
                    [child_lastname] => lacey
                )

            [duplicates] => Array
                (
                    [0] => 2
                    [1] => 3
                )

            [1] => Array
                (
                    [child_firstname] => daniel
                    [child_lastname] => lacey
                )

            [2] => Array
                (
                    [child_firstname] => sam
                    [child_lastname] => lacey
                )

        )

    [1] => Array
        (
            [parent_id] => 4
            [parent] => MrMrsDunn
            [0] => Array
                (
                    [child_firstname] => daniel
                    [child_lastname] => dunn
                )

        )

    [2] => Array
        (
            [parent_id] => 5
            [parent] => MrMrsParker
            [0] => Array
                (
                    [child_firstname] => sam
                    [child_lastname] => parker
                )

        )

)

预期结果:

Array
(
    [0] => Array
        (
            [parent_id] => 1
            [parent] => MrMrsLacey
            [0] => Array
                (
                    [child_firstname] => callum
                    [child_lastname] => lacey
                )

            [duplicates] => Array
                (
                    [0] => 2
                    [1] => 3
                )

            [1] => Array
                (
                    [child_firstname] => daniel
                    [child_lastname] => lacey
                )

            [2] => Array
                (
                    [child_firstname] => sam
                    [child_lastname] => lacey
                )    
        )
)

1 个答案:

答案 0 :(得分:1)

使用array_filter(),检查原始数组的每个元素是否存在duplicates密钥存在:

$with_duplicates = array_filter($temp, function($el) {
  return array_key_exists('duplicates', $el);
});

我在此代码段的原始版本中使用了isset,因为它曾经a bit faster而非array_key_exists检查。尽管如此,最后一个更清楚地显示了您的意图(并且由于某种原因,duplicate属性设置为NULL的元素不会失败。