1

I am having some trouble creating a JSON array. It should look like the following:

error : false
   duellid : 1
    questions : [
    {   questionid : xx
       question : lala
       answer : blabla },
    {   questionid : xx
       question : lala
       answer : blabla },
    {   questionid : xx
       question : lala
       answer : blabla }
]

Currently the problem is to create the top array questions within the json response :

$response["error"] = FALSE;
        $duellid = $duell["id"];

        $response["duell"] = $duellid;
        array_push($return_arr,$response);
        $response = array();
        $resultquestion = $db->getquestions($rows);
        while ($row = mysql_fetch_array($resultquestion)) {


            $response["question"]["id"] = $row["id"];
            $response["question"]["question"] = $row["question"];
            $response["question"]["answer"] = $row["answer"];
            $response["question"]["active"] = $row["active"];
            $response["question"]["minval"] = $row["minval"];
            $response["question"]["maxval"] = $row["maxval"];

            array_push($return_arr,$response);

        }


        echo json_encode($return_arr);

I think it's easy but I can't find the correct way.

2 Answers 2

3
$response = array();
$response["error"] = FALSE;
$duellid = $duell["id"];
$response["duell"] = $duellid;
$resultquestion = $db->getquestions($rows);      
$response['questions'] = array();  
while ($row = mysql_fetch_array($resultquestion)) { 

    $result = array(
        'questionid' =>   $row["id"],
        'question' => $row["question"],
        'answer' =>  $row["answer"],
        'active' =>  $row["active"],
        'minval' =>  $row["minval"],
        'maxval' =>  $row["maxval"]

    );
    $response['questions'][]  = $result;             

}         
echo json_encode($response);
Sign up to request clarification or add additional context in comments.

4 Comments

Damn you were faster, you could describe what you did and why, this would improve this answer.
@Rocky sorry.i will care from next
works perfect! thx! hopefully i'm able to add a new array within the questions array by myself ;)
@MineshPatel you still can edit it (: it may help other to understand.
0

Array push, push it on end of array. So result is without key questions. instead of:

array_push($return_arr,$response);

simply put it like that:

$response = array('questions'=>array());
$response["error"] = FALSE;
$response["duell"] = $duell["id"];
$resultquestion = $db->getquestions($rows);
while ($row = mysql_fetch_array($resultquestion)) {
    $r = array();
    $r["id"] = $row["id"];
    $r["question"] = $row["question"];
    $r["answer"] = $row["answer"];
    $r["active"] = $row["active"];
    $r["minval"] = $row["minval"];
    $r["maxval"] = $row["maxval"];
    $response["questions"][] = $r;

    // or nicer:
    $response["questions"][] = array(
        'question' => 'Your question',
        'answer' => 'any answer'
    );
}
echo json_encode($response);

Comments

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.