1

I'm trying to create a questionnaire app in Android and I can already get the questions from the database and show it as JSON Array but it will be displayed in this format:

{
    "result": [
        {
            "id_question": "1",
            "question_name": "Grade level",
            "choices": "Grade 11, Grade 12"
        },
        {
            "id_question": "2",
            "question_name": "Expected grade in this subject",
            "choices": "90-100, 75-89, 60-74, Below 60"
        }
    ]
}

But the library I'm using in Android is only accepting this kind of JSON format for showing the questions:

{
  "survey_properties": {
    "intro_message": "To get a reliable result for the evaluation, please respond to all questions.",
    "end_message": "Your answers have been recorded. <br>Thank you for taking the time to answer the evaluation."
  },
  "questions": [
    {
      "id_question": "1",
      "question_name": "Grade Level",
      "choices": [
        "Grade 11",
        "Grade 12"
      ]
    },
    {
      "id_question": "2",
      "question_name": "Expected Grade in this subject",
      "choices": [
        "90-100",
        "75-89",
        "60-74",
        "Below 60"
      ]
    }
  ]
}

How can I achieve this kind of output in PHP? This is the script I'm using:

$query = "SELECT * FROM question_test";

$r = mysqli_query($conn, $query);
$result = array();

while($row = mysqli_fetch_array($r)) {
    array_push($result,array(
        "id_question"=>$row['id_question'],
        "question_name"=>$row['question_name'],
        "choices"=>$row['choices']
        )
    );
}

echo json_encode(array("result"=>$result));

2
  • 4
    Loop through the array, explode the choices into an array et voila? Commented Sep 19, 2017 at 7:29
  • Thanks! Got the solution for the choices now from your and @tuan-duong 's answer. My problem now is how to add the survey_properties into the array Commented Sep 19, 2017 at 9:58

2 Answers 2

1

From your code, it could be modified like this:

$query = "SELECT * FROM question_test";

$r = mysqli_query($conn, $query);
$result = array();

while ($row = mysqli_fetch_array($r)) {
    array_push($result,array(
        "id_question"=>$row['id_question'],
        "question_name"=>$row['question_name'],
        "choices"=>explode(', ', $row['choices'])
    ));
}

echo json_encode(array("result"=>$result));

You have to add more information like survey_properties

Sign up to request clarification or add additional context in comments.

1 Comment

This worked! Thanks! :) how can I add the survey_properties? I tried to array_push the data but I can't seem to get the same output for the survey_properties like the format above.
0

What I'm trying to do here is put the "choices" into an array. Hope that works.

$query = "SELECT * FROM question_test";

$r = mysqli_query($conn, $query);
$result = array();
$choices = array();

while($row = mysqli_fetch_array($r)) {
    array_push($result,array(
        "id_question"=>$row['id_question'],
        "question_name"=>$row['question_name'],
        array_push($choices,array($row['choices'])
        )
    );
}

echo json_encode(array("result"=>$result));

1 Comment

This showed a Parse error: syntax error, unexpected ';', expecting ',' or ')'

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.