简体   繁体   中英

How to customize PHP array for desired JSON output

I have MySQL table named qbank having fields - qid, question_text, subject_id, option_01_text, option_01_is_correct, option_02_text, option_02_is_correct, option_03_text, option_03_is_correct, option_04_text, option_04_is_correct.

My desired JSON output is like this -

[{
        "qid": 5788,
        "question_text": "Which of the following is a traction epiphysis ?",
        "subject_id": 2,
        "options": [{
                "option_code": "A",
                "option_text": "Tibial condyles",
                "option_is_correct": 0
            },
            {
                "option_code": "B",
                "option_text": "Trochanter of femur",
                "option_is_correct": 1
            },
            {
                "option_code": "C",
                "option_text": "Coracoid process of scapula",
                "option_is_correct": 0
            },
            {
                "option_code": "D",
                "option_text": "head of femur",
                "option_is_correct": 0
            }
        ]
    },
    {
        "qid": 5789,
        "question_text": "Which is the most prominent spinous process",
        "subject_id": 2,
        "options": [{
                "option_code": "A",
                "option_text": "T1",
                "option_is_correct": 0
            },
            {
                "option_code": "B",
                "option_text": "C7",
                "option_is_correct": 1
            },
            {
                "option_code": "C",
                "option_text": "C6",
                "option_is_correct": 0
            },
            {
                "option_code": "D",
                "option_text": "L5",
                "option_is_correct": 0
            }
        ]
    }
]

My PHP code is -

$sql = "SELECT * FROM qbank Limit 2";
$query  = $mysqli->prepare($sql);
$query->execute();
$result = $query->get_result();

$output = [];
$options = [];

while ($row = $result->fetch_assoc()) {
    $output[] = [
        "qid"              => $row["qid"],
        "question_text"    => $row["question_text"],
        "subject_id"      => $row["subject_id"],
    ];

    $options[] = [
        "option_code" => "A",
        "option_text" => $row["option_01_text"],
        "option_is_correct" => $row["option_01_is_correct"],
    ];
    $options[] = [
        "option_code" => "B",
        "option_text" => $row["option_02_text"],
        "option_is_correct" => $row["option_02_is_correct"],
    ];
    $options[] = [
        "option_code" => "C",
        "option_text" => $row["option_03_text"],
        "option_is_correct" => $row["option_03_is_correct"],
    ];
    $options[] = [
        "option_code" => "D",
        "option_text" => $row["option_04_text"],
        "option_is_correct" => $row["option_04_is_correct"],
    ];
}
echo json_encode($output, JSON_PRETTY_PRINT);
echo json_encode($options, JSON_PRETTY_PRINT);

The output I am getting from PHP is -

[
    {
        "qid": 5788,
        "question_text": "Which of the following is a traction epiphysis ?",
        "subject_id": 2
    },
    {
        "qid": 5789,
        "question_text": "Which is the most prominent spinous process",
        "subject_id": 2
    }
][
    {
        "option_code": "A",
        "option_text": "Tibial condyles",
        "option_is_correct": 0
    },
    {
        "option_code": "B",
        "option_text": "Trochanter of femur",
        "option_is_correct": 1
    },
    {
        "option_code": "C",
        "option_text": "Coracoid process of scapula",
        "option_is_correct": 0
    },
    {
        "option_code": "D",
        "option_text": "head of femur",
        "option_is_correct": 0
    },
    {
        "option_code": "A",
        "option_text": "T1",
        "option_is_correct": 0
    },
    {
        "option_code": "B",
        "option_text": "C7",
        "option_is_correct": 1
    },
    {
        "option_code": "C",
        "option_text": "C6",
        "option_is_correct": 0
    },
    {
        "option_code": "D",
        "option_text": "L5",
        "option_is_correct": 0
    }
]

My approach was to somehow push $options inside $output , but even after several tries, I am not able to find the correct way.

You are creating two separate arrays with their own data, you need to build the question and add the options under that question...

while ($row = $result->fetch_assoc()) {
    $question = [
        "qid"              => $row["qid"],
        "question_text"    => $row["question_text"],
        "subject_id"       => $row["subject_id"],
        "options"          => []
    ];

    $question['options'][] = [
        "option_code" => "A",
        "option_text" => $row["option_01_text"],
        "option_is_correct" => $row["option_01_is_correct"],
    ];
    // And other options...

    $options[] = $question;
}
echo json_encode($options, JSON_PRETTY_PRINT);

One other point is that you may find normalizing your database tables useful. So you have a question and answer table. This allows you to have as many (or few) answers per question and stops repeating fields in 1 table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM