2017-02-08 17 views
0

刪除重複值,並創建一個單一的陣列已經試過到目前爲止我如何在PHP

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

我也曾嘗試使用循環自定義的方式來理清,但它是這麼多亂七八糟的。

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 
    ) 

期望的結果:

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

請分享你到目前爲止試過! –

+0

如果你正在使用'fetchAll()'把它改爲'fetchAll(PDO :: FETCH_ASSOC)',它會給你1個。 – MRustamzade

+1

我認爲這裏的question_id是一種獨特的。如果您從獲取該數據的位置更改MYSQL查詢,並且您將獲得具有不同答案的獨特數據。 – Naincy

回答

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']; 
    } 
} 
+0

完成:)希望它可以幫助 –

1

首先,我們將必須進行這些2排成一個陣列,其中每個鍵是一個子陣列的每一行的值。

因此,這將是這樣的:

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

不是爲每個關鍵,我們用「array_unique」只得到不同的值。 所以它看起來就像這樣:

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

最後乾脆檢查每個鍵陣還剩下1個或多個條目。 如果它只有一個簡單的自己替換爲一個值。

代碼:

$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

你爲什麼要這樣做?是什麼使這項工作?你能解釋你的答案嗎? – Martin

+0

那麼你通過鍵合併它們,比檢查是否有多個不同的值。如果是這樣的話,你只需要使它獨一無二,否則你只有一行,所以你只需將它設爲值而不是該值的數組。 –

+0

好的,所以編輯你的答案並添加這個有用的信息塊。到你的答案。歡呼':)' – Martin