2016-09-13 87 views
0

我有一個調查表,答案和用戶表。答案和調查表由user_id也用戶連接,調查與user_id連接。如何在php中顯示另一個數組中的一個數組?

我想爲調查顯示答案和用戶以及評論。評論在調查表內。

我想要得到的結果,在這個格式:

{ 
    result:1, 
    message:'success', 
    survey:[ 
    { 
     answers: 
     [ 

     ], 
     user: 
     { 

     } 
     comment: 
    }, 
    { 
     answers: 
     [ 

     ], 
     user: 
     { 

     } 
     comment: 
    } 
    ] 
} 

getAllSurveyByDoctor功能:

function getAllSurveyByDoctor($user_id) 
    { 
     $database = new SurveyDatabase(SurveyConstants::DBHOST,SurveyConstants::DBUSER,SurveyConstants::DBPASS,SurveyConstants::DBNAME); 
     $dbConnection = $database->getDB(); 

     $stmt = $dbConnection->prepare("SELECT * from survey where `user_id` = ?"); 
     $stmt->execute(array($user_id)); 
     $survey = $stmt->fetchAll(PDO::FETCH_ASSOC); 

     $surveys = array(); 
     $answers = array(); 

     if (count($survey) > 0) { 

      foreach($survey as $row) 
      { 

       $stmt = $dbConnection->prepare("SELECT answer.answer_id, answer.survey_id, answer.question_id, answer.rating, answer.user_id, question.question_id, question.question, question.type FROM `answer` INNER JOIN `question` ON 
answer.question_id = question.question_id WHERE answer.user_id = ? and answer.survey_id = ?"); 

       $s_id = $row['survey_id']; 

       $stmt->execute(array($user_id,$s_id)); 
       $answer = $stmt->fetchAll(PDO::FETCH_ASSOC); 

       $stmt = $dbConnection->prepare("SELECT users.user_id,users.email_id,users.pass,users.address,users.name,survey.survey_id,survey.user_id FROM `users` INNER JOIN `survey` ON users.user_id = survey.user_id WHERE users.user_id = ?"); 

       $stmt->execute(array($user_id)); 
       $user = $stmt->fetch(PDO::FETCH_ASSOC); 

       $comment = $row['comment']; 

      // $surveys[] = $row; 
       $answers = $answer; 
       $surveys[] = $answers; 
       $surveys[] = $user; 
       $surveys[] = $comment; 
      } 

      $response = array("status" => 1, "message" => "Success", "surveys" => $surveys); 
      return json_encode($response); 
     } 

     else { 
      $response = array("status"=>-1,"message"=>"surveys list is empty"); 
      return json_encode($response); 
     } 
    } 

我得到的結果是這樣的:

{ 
    "status": 1, 
    "message": "Success", 
    "surveys": [ 
    [ 
     { 
     "answer_id": "50", 
     "survey_id": "35", 
     "question_id": "1", 
     "rating": "4", 
     "user_id": "9", 
     "question": "Scheduling appointments", 
     "type": "rating" 
     }, 
     { 
     "answer_id": "51", 
     "survey_id": "35", 
     "question_id": "2", 
     "rating": "6", 
     "user_id": "9", 
     "question": "Office environment", 
     "type": "rating" 
     } 
    ], 
    { 
     "user_id": "9", 
     "email_id": "[email protected]", 
     "pass": "user7", 
     "address": "miraroad", 
     "name": "user7", 
     "survey_id": "35" 
    }, 
    "0", 
    [], 
    { 
     "user_id": "9", 
     "email_id": "[email protected]", 
     "pass": "user7", 
     "address": "miraroad", 
     "name": "user7", 
     "survey_id": "35" 
    }, 
    "0", 
    [ 
     { 
     "answer_id": "54", 
     "survey_id": "37", 
     "question_id": "1", 
     "rating": "4", 
     "user_id": "9", 
     "question": "Scheduling appointments", 
     "type": "rating" 
     }, 
     { 
     "answer_id": "55", 
     "survey_id": "37", 
     "question_id": "2", 
     "rating": "6", 
     "user_id": "9", 
     "question": "Office environment", 
     "type": "rating" 
     } 
    ], 
    { 
     "user_id": "9", 
     "email_id": "[email protected]", 
     "pass": "user7", 
     "address": "miraroad", 
     "name": "user7", 
     "survey_id": "35" 
    }, 
    "0", 
    [], 
    { 
     "user_id": "9", 
     "email_id": "[email protected]", 
     "pass": "user7", 
     "address": "miraroad", 
     "name": "user7", 
     "survey_id": "35" 
    }, 
    "csdcs", 
    [ 
     { 
     "answer_id": "72", 
     "survey_id": "49", 
     "question_id": "7", 
     "rating": "5", 
     "user_id": "9", 
     "question": "Doctor listens and answers questions", 
     "type": "rating" 
     }, 
     { 
     "answer_id": "73", 
     "survey_id": "49", 
     "question_id": "6", 
     "rating": "5", 
     "user_id": "9", 
     "question": "Doctor explains medical condition(s)", 
     "type": "rating" 
     } 
    ], 
    { 
     "user_id": "9", 
     "email_id": "[email protected]", 
     "pass": "user7", 
     "address": "miraroad", 
     "name": "user7", 
     "survey_id": "35" 
    }, 
    "", 
    [ 
     { 
     "answer_id": "74", 
     "survey_id": "50", 
     "question_id": "8", 
     "rating": "5", 
     "user_id": "9", 
     "question": "Doctor spends enough time with patients", 
     "type": "rating" 
     }, 
     { 
     "answer_id": "75", 
     "survey_id": "50", 
     "question_id": "3", 
     "rating": "5", 
     "user_id": "9", 
     "question": "Staff helpfulness", 
     "type": "rating" 
     } 
    ], 
    { 
     "user_id": "9", 
     "email_id": "[email protected]", 
     "pass": "user7", 
     "address": "miraroad", 
     "name": "user7", 
     "survey_id": "35" 
    }, 
    "", 
    [ 
     { 
     "answer_id": "76", 
     "survey_id": "51", 
     "question_id": "7", 
     "rating": "5", 
     "user_id": "9", 
     "question": "Doctor listens and answers questions", 
     "type": "rating" 
     }, 
     { 
     "answer_id": "77", 
     "survey_id": "51", 
     "question_id": "6", 
     "rating": "5", 
     "user_id": "9", 
     "question": "Doctor explains medical condition(s)", 
     "type": "rating" 
     } 
    ], 
    { 
     "user_id": "9", 
     "email_id": "[email protected]", 
     "pass": "user7", 
     "address": "miraroad", 
     "name": "user7", 
     "survey_id": "35" 
    }, 
    "", 
    [ 
     { 
     "answer_id": "78", 
     "survey_id": "52", 
     "question_id": "8", 
     "rating": "5", 
     "user_id": "9", 
     "question": "Doctor spends enough time with patients", 
     "type": "rating" 
     }, 
     { 
     "answer_id": "79", 
     "survey_id": "52", 
     "question_id": "5", 
     "rating": "5", 
     "user_id": "9", 
     "question": "Trust in doctor's decision", 
     "type": "rating" 
     } 
    ], 
    { 
     "user_id": "9", 
     "email_id": "[email protected]", 
     "pass": "user7", 
     "address": "miraroad", 
     "name": "user7", 
     "survey_id": "35" 
    }, 
    "", 
    [ 
     { 
     "answer_id": "80", 
     "survey_id": "53", 
     "question_id": "1", 
     "rating": "5", 
     "user_id": "9", 
     "question": "Scheduling appointments", 
     "type": "rating" 
     }, 
     { 
     "answer_id": "81", 
     "survey_id": "53", 
     "question_id": "7", 
     "rating": "5", 
     "user_id": "9", 
     "question": "Doctor listens and answers questions", 
     "type": "rating" 
     } 
    ], 
    { 
     "user_id": "9", 
     "email_id": "[email protected]", 
     "pass": "user7", 
     "address": "miraroad", 
     "name": "user7", 
     "survey_id": "35" 
    }, 
    "", 
    [ 
     { 
     "answer_id": "82", 
     "survey_id": "54", 
     "question_id": "6", 
     "rating": "5", 
     "user_id": "9", 
     "question": "Doctor explains medical condition(s)", 
     "type": "rating" 
     }, 
     { 
     "answer_id": "83", 
     "survey_id": "54", 
     "question_id": "3", 
     "rating": "5", 
     "user_id": "9", 
     "question": "Staff helpfulness", 
     "type": "rating" 
     } 
    ], 
    { 
     "user_id": "9", 
     "email_id": "[email protected]", 
     "pass": "user7", 
     "address": "miraroad", 
     "name": "user7", 
     "survey_id": "35" 
    }, 
    "" 
    ] 
} 

我得到一些什麼格式與我想要的相同。之後,我得到了答案數組,用戶和評論。但我怎麼能命名呢?無法從此結果中識別出答案,用戶和評論。我想命名它。我怎麼能夠?謝謝。

+0

$ surveys [] =正在爲數組順序添加一個新值。如果你想命名它。使用鑰匙計數器。像$ survey [$ i] ['user_id'] = $ user,$ surveys [$ i] ['comment'] = $ comment。確保增加計數器。 –

+0

亞歷克西斯的解決方案也適用。一種不需要櫃檯的方法。 –

回答

3

您可以爲每個調查簡單地創建一個關聯數組。在這個關聯數組中,分配你的鍵和你的值。下面是它的外觀:

function getAllSurveyByDoctor($user_id) 
    { 
     $database = new SurveyDatabase(SurveyConstants::DBHOST,SurveyConstants::DBUSER,SurveyConstants::DBPASS,SurveyConstants::DBNAME); 
     $dbConnection = $database->getDB(); 

     $stmt = $dbConnection->prepare("SELECT * from survey where `user_id` = ?"); 
     $stmt->execute(array($user_id)); 
     $survey = $stmt->fetchAll(PDO::FETCH_ASSOC); 

     $surveys = array(); 
     $answers = array(); 

     if (count($survey) > 0) { 

      foreach($survey as $row) 
      { 

       $stmt = $dbConnection->prepare("SELECT answer.answer_id, answer.survey_id, answer.question_id, answer.rating, answer.user_id, question.question_id, question.question, question.type FROM `answer` INNER JOIN `question` ON 
answer.question_id = question.question_id WHERE answer.user_id = ? and answer.survey_id = ?"); 

       $s_id = $row['survey_id']; 

       $stmt->execute(array($user_id,$s_id)); 
       $answer = $stmt->fetchAll(PDO::FETCH_ASSOC); 

       $stmt = $dbConnection->prepare("SELECT users.user_id,users.email_id,users.pass,users.address,users.name,survey.survey_id,survey.user_id FROM `users` INNER JOIN `survey` ON users.user_id = survey.user_id WHERE users.user_id = ?"); 

       $stmt->execute(array($user_id)); 
       $user = $stmt->fetch(PDO::FETCH_ASSOC); 

       $comment = $row['comment']; 

      // $surveys[] = $row; 

       //We create the "survey" associative array 
       $survey = []; 
       $answers = $answer; 
       $survey['answers']= $answers; 
       $survey['user'] = $user; 
       $survey['comment']=$comment; 
       //We add the survey array to the survey 
       $surveys[] = $survey; 
      } 

      $response = array("status" => 1, "message" => "Success", "surveys" => $surveys); 
      return json_encode($response); 
     } 

     else { 
      $response = array("status"=>-1,"message"=>"surveys list is empty"); 
      return json_encode($response); 
     } 
    } 
+0

是的懂了..謝謝你.. @AlexisCôté – Sid

+0

不客氣@Sid –

相關問題