Shahbaz
Shahbaz

Reputation: 3463

How to remove duplicate values and create a single array in PHP

I have tried so far

$result = array_map("unserialize", array_unique(array_map("serialize", $surveyData)));

I have also tried to sort out in custom way using loops but it is so much messy.

Array
(
    [0] => Array
    (
        [question_id] => 8
        [answer] => 10 patients
        [question_type] => 1
        [question] => How many diabetes patients do you see per month?
        [question_no] => 3
        [survey_id] => 2
        [name] => Health Pharmacist Digital Advantage
        [description] => Free One Year Subscription to Diabetes Health Pharmacist Digital Advantage magazine
    )

[1] => Array
    (
        [question_id] => 8
        [answer] => 30 patients
        [question_type] => 1
        [question] => How many diabetes patients do you see per month?
        [question_no] => 3
        [survey_id] => 2
        [name] => Health Pharmacist Digital Advantage
        [description] => Free One Year Subscription to Diabetes Health Pharmacist Digital Advantage magazine
    )

Desired result:

Array
(
    [0] => Array
    (
    [question_id] => 8
    [answer] => Array(
        [0] => 10 patients,
        [1] => 30 patients,
    )
    [question_type] => 1
    [question] => How many diabetes patients do you see per month?
    [question_no] => 3
    [survey_id] => 2
    [name] => Health Pharmacist Digital Advantage
    [description] => Free One Year Subscription to Diabetes Health Pharmacist Digital Advantage magazine
)

Upvotes: 0

Views: 88

Answers (2)

Milot Dauti
Milot Dauti

Reputation: 11

First we will have to make these 2 Rows into one array, where each key is a sub array with the values of each row.

So it will look like this:

array
    (
        [question_id] => array( 8, 8 ),
        [answer] => array( "10 patients", "30 patients" ),
        [question_type] => array( 1, 1 ),
        [question] => array( "How many diabetes patients do you see per month?", "How many diabetes patients do you see per month?" ),
        [question_no] => array( 3, 3 ),
        [survey_id] => array( 2, 2),
        [name] => array( "Health Pharmacist Digital Advantage", "Health Pharmacist Digital Advantage"),
        [description] => array("Free One Year Subscription to Diabetes Health Pharmacist Digital Advantage magazine","Free One Year Subscription to Diabetes Health Pharmacist Digital Advantage magazine")
    )

than for each key, we use "array_unique" to only get distinct values. so it will look like this:

array
        (
            [question_id] => array( 8),
            [answer] => array( "10 patients", "30 patients" ),
            [question_type] => array( 1 ),
            [question] => array( "How many diabetes patients do you see per month?" ),
            [question_no] => array( 3 ),
            [survey_id] => array( 2 ),
            [name] => array( "Health Pharmacist Digital Advantage"),
            [description] => array("Free One Year Subscription to Diabetes Health Pharmacist Digital Advantage magazine")
        )

finally simply check if each key-Array has 1 or more entries left. If it only has one simply replace itself with the one value.

Code:

$aArrayToParse = array
(
    [0] => array
    (
        [question_id] => 8
        [answer] => "10 patients"
        [question_type] => 1
        [question] => "How many diabetes patients do you see per month?"
        [question_no] => 3
        [survey_id] => 2
        [name] => "Health Pharmacist Digital Advantage"
        [description] => "Free One Year Subscription to Diabetes Health Pharmacist Digital Advantage magazine"
    ),

[1] => array
    (
        [question_id] => 8
        [answer] => "30 patients"
        [question_type] => 1
        [question] => "How many diabetes patients do you see per month?"
        [question_no] => 3
        [survey_id] => 2
        [name] => "Health Pharmacist Digital Advantage"
        [description] => "Free One Year Subscription to Diabetes Health Pharmacist Digital Advantage magazine"
    )
);

$aTempArray = array();

// first let's get it all into one array
foreach( $aArrayToParse as $iKey => $aOneDataSet ){
        foreach( $aOneDataSet as $iDataKey => $mDataValue ){
                if(!isset($aTempArray[$iDataKey]))
                {
                        $aTempArray[$iDataKey] = array();
                }
                $aTempArray[$iDataKey][] = $mDataValue;
        }
}

// than check and remove all the duplicants
foreach($aTempArray as $iKey => &$aData ){
        $aData = array_unique($aData);
        if( count($aData) == 1 )
        {
                $aData = $aData[0];
        }
}

Upvotes: 1

Florian Rubel
Florian Rubel

Reputation: 124

/**
 * empty array for example. This has to be your data.
 */
$questions = [];

/**
 * The target array, where we will use the question id to prevent duplicate questions.
 */
$mergedQuestions = [];

foreach($questions as $question) {
    // save the current question id to a variable to make the code more readable.
    $currentQuestionId = $question['question_id'];

    // if the array key with the question id of the current question exists in the target array,
    // we can just put the answer to the answer array of the target array with the question id
    // as the array key.
    if(array_key_exists($currentQuestionId, $mergedQuestions) {
        $mergedQuestions[$currentQuestionId]['answer'][] = $question['answer'];
    }else {
        // if we don't find the question id as an array key in the target array,
        // we can be sure its not there yet and just save the question there.
        $mergedQuestions[$currentQuestionId] = $question;

        // we want to have the answer field in the question as an array field and thats
        // what we are doing here.
        $mergedQuestions[$currentQuestionId]['answer'] = [$question['answer'];
    }
}

Upvotes: 3

Related Questions