如何删除数组中的重复条目,但通过覆盖保留其值

时间:2017-03-07 15:39:02

标签: php arrays

我有以下数组:

Array
(
    [0] => Array
        (
            [contact_id] => 1 // *
            [exact_email_found] => 0
            [half_email_found] => 0
            [possible_by_postal_code] => 1
            [found_first_name] => 0
            [found_last_name] => 0
        )

    [1] => Array
        (
            [contact_id] => 3
            [exact_email_found] => 0
            [half_email_found] => 0
            [possible_by_postal_code] => 0
            [found_first_name] => 1
            [found_last_name] => 0
        )

    [2] => Array
        (
            [contact_id] => 2 // **
            [exact_email_found] => 0
            [half_email_found] => 0
            [possible_by_postal_code] => 0
            [found_first_name] => 1
            [found_last_name] => 0
        )

    [3] => Array
        (
            [contact_id] => 1 // *
            [exact_email_found] => 0
            [half_email_found] => 0
            [possible_by_postal_code] => 0
            [found_first_name] => 0
            [found_last_name] => 1
        )

    [4] => Array
        (
            [contact_id] => 2 // **
            [exact_email_found] => 1
            [half_email_found] => 0
            [possible_by_postal_code] => 0
            [found_first_name] => 0
            [found_last_name] => 0
        )

)

我正在尝试使该数组“合并但保留被覆盖的值”。

让我进一步解释。如果查看数组[0],可以看到[possible_by_postal_code] => 1。这个有一个[contact_id] => 1。如果你看下面你可以看到数组[3]具有相同的ID,而不是[possible_by_postal_code] => 0[found_last_name] => 1

在新阵列中,我试图完成以下任务:

Array
(
    [0] => Array
        (
            [contact_id] => 1 // *
            [exact_email_found] => 0
            [half_email_found] => 0
            [possible_by_postal_code] => 1
            [found_first_name] => 0
            [found_last_name] => 1
        )

    [1] => Array
        (
            [contact_id] => 3
            [exact_email_found] => 0
            [half_email_found] => 0
            [possible_by_postal_code] => 0
            [found_first_name] => 1
            [found_last_name] => 0
        )

    [2] => Array
        (
            [contact_id] => 2 // **
            [exact_email_found] => 1
            [half_email_found] => 0
            [possible_by_postal_code] => 0
            [found_first_name] => 1
            [found_last_name] => 0
        )   

)

具有contact_id = 1的数组[0]现在只存在一次,而值possible_by_postal_codefound_last_name的值为1

注意1:这只是一个例子,我的数组有超过200个对象,而且它是随机的。这意味着contact_id = 5可以位于0189

注意2:重复条目可能不仅仅是2

  

主.php文件

$mTempDBUsers = getDataOfTempDB();

foreach($mTempDBUsers as $index => $mCurrentUser){


    $allUserData = [];
    $allUserConfig = [];

    $mCurrentUserEmail = $mCurrentUser['email'];
    $mCurrentPostalCode = $mCurrentUser['postal_code'];
    $mCurrentLastName   = $mCurrentUser['lastname'];
    $mCurrentFirstName  = $mCurrentUser['firstname'];

    if($mCurrentPostalCode == "EX23 8JS") {

        $current_user_config = array(
            "exact_email_found" => 0,
            "half_email_found" => 0,
            "possible_by_postal_code" => 0,
            "found_first_name" => 0,
            "found_last_name" => 0,
        );


        if (tryToFindUserWithEmail($mCurrentUserEmail, "")) {

            $current_user_config['exact_email_found'] = 1;

            $tempArray = tryToFindUserWithEmail($mCurrentUserEmail, $current_user_config);

            $allUserData = array_merge($allUserData, $tempArray);

        }

        if (tryToFindUserWithEmailHalf($mCurrentUserEmail, "")) {

            $current_user_config = array(
                "exact_email_found" => 0,
                "half_email_found" => 0,
                "possible_by_postal_code" => 0,
                "found_first_name" => 0,
                "found_last_name" => 0,
            );

            $current_user_config['half_email_found'] = 1;


            $tempArray = tryToFindUserWithEmailHalf($mCurrentUserEmail, $current_user_config);

            $allUserData = array_merge($allUserData, $tempArray);

        }


        if (tryToFindUserBy("mailing_postal_code", $mCurrentPostalCode, "")) {


            $current_user_config = array(
                "exact_email_found" => 0,
                "half_email_found" => 0,
                "possible_by_postal_code" => 0,
                "found_first_name" => 0,
                "found_last_name" => 0,
            );

            $current_user_config['possible_by_postal_code'] = 1;

            $tempArray = tryToFindUserBy("mailing_postal_code", $mCurrentPostalCode, $current_user_config);

            $allUserData = array_merge($allUserData, $tempArray);


        }

        if (tryToFindUserBy("lastname", str_replace(' ', '', $mCurrentLastName), "")) {

            $current_user_config = array(
                "exact_email_found" => 0,
                "half_email_found" => 0,
                "possible_by_postal_code" => 0,
                "found_first_name" => 0,
                "found_last_name" => 0,
            );

            $current_user_config['found_last_name'] = 1;

            $tempArray = tryToFindUserBy("lastname", str_replace(' ', '', $mCurrentLastName), $current_user_config);

            $allUserData = array_merge($allUserData, $tempArray);

        }

        if(tryToFindUserBy("firstname", $mCurrentFirstName, "")) {


            $current_user_config = array(
                "exact_email_found" => 0,
                "half_email_found" => 0,
                "possible_by_postal_code" => 0,
                "found_first_name" => 0,
                "found_last_name" => 0,
            );


            $current_user_config['found_first_name'] = 1;

            $tempArray = tryToFindUserBy("firstname", $mCurrentFirstName, $current_user_config);

            $allUserData = array_merge($allUserData, $tempArray);

        }

        print_r($allUserData);



    }

}
  

功能:tryToFindUserBy

function tryToFindUserBy($mTypeOfSearch, $mInfoOfCurrentUser, $current_user_config){

    global $connection;

    $usersArray = [];

    $query_select = "SELECT * FROM main_db WHERE upper($mTypeOfSearch) = upper('$mInfoOfCurrentUser')";

    $result = mysqli_query($connection,$query_select);

    $num_rows = $result->num_rows;

    if($num_rows >= 1){
        for($i = 0; $i < $num_rows; $i++) {
            $usersArray[$i] = mysqli_fetch_array($result, MYSQLI_ASSOC);

            if($current_user_config != "") {
                foreach ($current_user_config as $index => $config) {
                    $usersArray[$i][$index] = $config;
                }
            }
        }

        return $usersArray;
    }
    else{
        return false;
    }

}
  

功能:tryToFindUserWithEmail

function tryToFindUserWithEmail($mEmail, $current_user_config){

    global $connection;

    $usersArray = [];

    $query_select = "SELECT * FROM main_db WHERE upper(email) = upper('$mEmail')";

    $result = mysqli_query($connection,$query_select);

    $num_rows = $result->num_rows;

    if($num_rows >= 1){
        for($i = 0; $i < $num_rows; $i++) {
            $usersArray[$i] = mysqli_fetch_array($result, MYSQLI_ASSOC);

            if($current_user_config != "") {
                foreach ($current_user_config as $index => $config) {
                    $usersArray[$i][$index] = $config;
                }
            }
        }

        return $usersArray;
    }
    else{
        return false;
    }

}
  

功能:tryToFindUserWithEmailHalf

function tryToFindUserWithEmailHalf($mEmail, $current_user_config){

    global $connection;

    $usersArray = [];

    $mEmail = explode("@", $mEmail)[0];

    $query_select = "SELECT * FROM main_db WHERE upper(email) LIKE upper('$mEmail%')";


    $result = mysqli_query($connection,$query_select);

    $num_rows = $result->num_rows;

    if($num_rows >= 1){
        for($i = 0; $i < $num_rows; $i++) {
            $usersArray[$i] = mysqli_fetch_array($result, MYSQLI_ASSOC);

            if($current_user_config != "") {
                foreach ($current_user_config as $index => $config) {
                    $usersArray[$i][$index] = $config;
                }
            }
        }

        return $usersArray;
    }
    else{
        return false;
    }

}
  

函数getDataOfTempDB

function getDataOfTempDB(){

    global $connection;

    $usersArray = [];

    $query_select = "SELECT * FROM temp_db";

    $result = mysqli_query($connection,$query_select);

    $num_rows = $result->num_rows;


    for($i = 0; $i < $num_rows; $i++) {
        $usersArray[] = mysqli_fetch_array($result, MYSQLI_ASSOC);
    }


    mysqli_free_result($result);

    return $usersArray;

}

1 个答案:

答案 0 :(得分:0)

<head>
  <meta charset="utf-8">
  <title>Users</title>
  <link rel="stylesheet" href="bootstrap.css">
  <link rel="stylesheet" href="bootstrap.min.css">
  
  <!-- my style sheet -->
  <link rel="stylesheet" href="style.css">
</head>

结果将是这样的:



    $newArray = [];
    foreach ($myArray as $key => $row) {
        if (isset($newArray[$row['contact_id']])) {
            foreach ($row as $k => $r) {
                if($r ){
                    $newArray[$row['contact_id']][$k] = $r;
                }
            }
        } else{
            $newArray[$row['contact_id']] = $row;
        }
    }