title: How to show one array in another array in php?
body: <p>I have table of a survey,answer and user. Answer and survey table is linked by user_id also user and survey is linked with user_id. </p>
  
  <p>I want to show answers and user and comment for a survey. comment is inside survey table. </p>
  
  <p>I want to get the result in this format:</p>
  
  <pre><code>   { 
      result:1,
      message:'success',
      survey:[ 
      {
          answers:
          [
  
          ],
          user:
          {
  
          }
          comment:
      },
      {
          answers:
          [
  
          ],
          user:
          {
  
          }
          comment:
      }   
      ]  
  }
  </code></pre>
  
  <p>getAllSurveyByDoctor function:</p>
  
  <pre><code>    function getAllSurveyByDoctor($user_id)
      {
          $database = new SurveyDatabase(SurveyConstants::DBHOST,SurveyConstants::DBUSER,SurveyConstants::DBPASS,SurveyConstants::DBNAME);
          $dbConnection = $database-&gt;getDB();
  
          $stmt = $dbConnection-&gt;prepare("SELECT * from survey where `user_id` = ?");
          $stmt-&gt;execute(array($user_id));
          $survey = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC);
  
          $surveys = array();
          $answers = array();
  
          if (count($survey) &gt; 0) {
  
              foreach($survey as $row)
              {
  
                  $stmt = $dbConnection-&gt;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-&gt;execute(array($user_id,$s_id));
                  $answer = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC);
  
                  $stmt = $dbConnection-&gt;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-&gt;execute(array($user_id));
                  $user = $stmt-&gt;fetch(PDO::FETCH_ASSOC);
  
                  $comment = $row['comment'];
  
               //   $surveys[] = $row;
                  $answers = $answer;
                  $surveys[] = $answers;
                  $surveys[] = $user;
                  $surveys[] = $comment;
              }
  
              $response = array("status" =&gt; 1, "message" =&gt; "Success", "surveys" =&gt; $surveys);
              return json_encode($response);
          }
  
          else {
              $response = array("status"=&gt;-1,"message"=&gt;"surveys list is empty");
              return json_encode($response);
          }
      }
  </code></pre>
  
  <p>I get the result like this:</p>
  
  <pre><code>    {
    "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": "user7@gmail.com",
        "pass": "user7",
        "address": "miraroad",
        "name": "user7",
        "survey_id": "35"
      },
      "0",
      [],
      {
        "user_id": "9",
        "email_id": "user7@gmail.com",
        "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": "user7@gmail.com",
        "pass": "user7",
        "address": "miraroad",
        "name": "user7",
        "survey_id": "35"
      },
      "0",
      [],
      {
        "user_id": "9",
        "email_id": "user7@gmail.com",
        "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": "user7@gmail.com",
        "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": "user7@gmail.com",
        "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": "user7@gmail.com",
        "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": "user7@gmail.com",
        "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": "user7@gmail.com",
        "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": "user7@gmail.com",
        "pass": "user7",
        "address": "miraroad",
        "name": "user7",
        "survey_id": "35"
      },
      ""
    ]
  }
  </code></pre>
  
  <p>I am getting some what format same as I want. I am getting answers array, user and comment after that. But how I can name it? Can not identify from this result that where are answers, user and comment. I want to name it. How can I? Thank you.</p>