0

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
)
7
  • 4
    Please share what you have tried so far! Commented Feb 8, 2017 at 16:19
  • if you are using fetchAll() change it to fetchAll(PDO::FETCH_ASSOC) and it will give you just 1. Commented Feb 8, 2017 at 16:23
  • 1
    I think here question_id is kind of unique. If you change in MYSQL query from where you getting this data and you will get unique data for question with different answers. Commented Feb 8, 2017 at 16:25
  • @TheCodesee, I have edited the question, I have tried in custom way, also many solutions from stakcoverflow Commented Feb 8, 2017 at 16:25
  • 2
    Could you edit your question to also show your MySQL table(s) (using SHOW TABLE ) as it'd be far easier and quicker to sort this data correctly in MySQL rather than doing secondary sorting with PHP. Commented Feb 8, 2017 at 16:30

2 Answers 2

3
/**
 * 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'];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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];
        }
}

3 Comments

Why would you do this? What makes this work? Can you explain your answer, please.
Well you merge them by key, than check if there are more than one distinct Value. if so, you just make it unique else you only have one row so you just make it the value instead of an array of that value.
ok, so edit your answer and add this useful nugget of info. to your answer. cheers :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.