donglu5000 2016-10-25 13:06
浏览 25
已采纳

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

$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);
}

Print out:

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
        )
)

What I want to be able to print out: (needs to should the 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条回答 默认 最新

  • doubing3662 2016-10-25 13:13
    关注

    You can add values to index based array and then reset it: 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);
    

    Prints:

    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条)

报告相同问题?

悬赏问题

  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题