合并/组数组值以停止具有完全相同的全名的重复父项

时间:2016-10-25 13:06:47

标签: php arrays

$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 & Mrs O'brian", "child_firstname" => "bruce", "child_lastname" => "O'brian");
$array[] = array("parent_id" => 4, "parent" => "Mr & Mrs Obrian", "child_firstname" => "dave", "child_lastname" => "O'brian");


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"]);
    $temp[] = array("parent_id" => $item["parent_id"], 
                    "parent" => $parent, 
                    "child_firstname" => $child_firstname, 
                    "child_lastname" => $child_lastname);
}

打印出来:

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

    [1] => Array
        (
            [parent_id] => 2
            [parent] => MrMrsLacey
            [child_firstname] => daniel
            [child_lastname] => lacey
        )

    [2] => Array
        (
            [parent_id] => 3
            [parent] => MrMrsObrian
            [child_firstname] => bruce
            [child_lastname] => Obrian
        )

    [3] => Array
        (
            [parent_id] => 4
            [parent] => MrMrsObrian
            [child_firstname] => dave
            [child_lastname] => Obrian
        )
)

我希望能够打印出来:(需要duplicated_id

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

    [1] => Array (
            [parent_id] => 3
            [duplicated_id] => 4
            [parent] => MrMrsObrian
            [0] => Array (
                [child_firstname] => bruce
                [child_lastname] => Obrian
            )
            [1] => Array (
                [child_firstname] => dave
                [child_lastname] => Obrian
            )
        )
)

2 个答案:

答案 0 :(得分:1)

您可以向基于索引的数组添加值,然后重置它:Eval

<?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 & Mrs O'brian", "child_firstname" => "bruce", "child_lastname" => "O'brian");
$array[] = array("parent_id" => 4, "parent" => "Mr & Mrs Obrian", "child_firstname" => "dave", "child_lastname" => "O'brian");


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]['duplicated_id'] = $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
                )

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

        )

    [1] => Array
        (
            [parent_id] => 3
            [parent] => MrMrsObrian
            [0] => Array
                (
                    [child_firstname] => bruce
                    [child_lastname] => Obrian
                )

            [duplicated_id] => 4
            [1] => Array
                (
                    [child_firstname] => dave
                    [child_lastname] => Obrian
                )

        )

)

答案 1 :(得分:0)

如果您对通过父ID索引新数组感到满意(这应该无关紧要),您可以这样做:

foreach($array as $delta => $item){
  [...]
  $temp[$item['parent_id']]['parent_id'] = $item["parent_id"];
  $temp[$item['parent_id']]['parent'] = $item["parent"];

  $temp[$item['parent_id']][$delta] = array(
    "child_firstname" => $child_firstname, 
    "child_lastname" => $child_lastname);
  );
}

这会导致父级的parent_idparent键被重复覆盖,但这无关紧要;结果应该是你想要的。

如果可能的话,我会将孩子们放在他们自己的钥匙下,以便轻松迭代。在迭代孩子的那一刻,你必须测试每个键以确保它是数字的!