1

this is my code:

  $db = connect_mysqli();

  $response = array();

  $sql = "SELECT * FROM questions ORDER BY RAND ()";
  $result = $db->query($sql);

  while($row = $result->fetch_array(MYSQL_ASSOC))
  {
    $response['answers'][$row['id']] = array('question_id'=>$row['id'], 'option_id'=>null);
  }

  echo json_encode($response);

And this is the response:

{
  answers: {
    1: {
      question_id: "1",
      option_id: null
    },
    2: {
      question_id: "2",
      option_id: null
    },
    3: {
      question_id: "3",
      option_id: null
    },
    4: {
      question_id: "4",
      option_id: null
    }
  }
}

How to make the response always return JSON Array? not JSON Object like that. Sometimes the response is return JSON Array and in some part, return JSON Object. I want all response tobe JSON Array.

So, it should be like this:

{
  answers: [
    1: {
      question_id: "1",
      option_id: null
    },
    2: {
      question_id: "2",
      option_id: null
    },
    3: {
      question_id: "3",
      option_id: null
    },
    4: {
      question_id: "4",
      option_id: null
    }
  ]
}
4
  • Try change $response['answers'][$row['id']] to $response['answers'][] Commented Dec 30, 2017 at 4:33
  • i need this as a key $response['answers'][$row['id']] @MrHery Commented Dec 30, 2017 at 4:34
  • Then try fetch from database as an object, not array. Commented Dec 30, 2017 at 4:38
  • If you need the key to be $row['id'], then the json output you got is correct. You cannot have an array that has keys as values in json. If you want to have it return an array then embed the id into the object instead. Commented Dec 30, 2017 at 10:03

3 Answers 3

1
$response['answers'][$row['id']] = array('question_id'=>$row['id'], 'option_id'=>null);

This previous line should be changed to:

$response['answers'][] = array('question_id'=>$row['id'], 'option_id'=>null);

The problem comes from the fact that you do not start the array at an index of 0.

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

5 Comments

I need this $response['answers'][$row['id']] as a key
If you need a key, it's an object, not a array, you need. Arrays always start from a zero index and go up from there.
Hmm, then how i can show it? if is an JSON Array answers[1].option_id, how for an JSON Object?
This is how would access it an object instead of an array: $answers[1]->option_id
In JavaScript accessing an array or an object is the same notation: answers[1].option_id should work. See this: jsfiddle.net/loic294/u2shu0Ls
0

does it help you

   $response = json_encode($response);

Comments

0

cast the $row['id'] as string to get desired result.

$db = connect_mysqli();

$response = array();

$sql = "SELECT * FROM questions ORDER BY RAND ()";
$result = $db->query($sql);

while($row = $result->fetch_array(MYSQL_ASSOC))
 {
   $response['answers'][(string)$row['id']] = array('question_id'=>$row['id'], 'option_id'=>null);
 }

echo json_encode($response);

2 Comments

No, it's still same like before :(
@DimasAdiAndrea please validate the json you are expecting.

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.