0

I'm trying to make a form with a bunch of questions which can be added to a radioBox, checkBox etc. I have made a .json file containing the questions and possible answers like this (this is just a small part of the file):

{
"questions": {
    "0": {
            "question":"How many products?",
            "answers":["Less than 1000", "More than 1000", "More than 10000"],
            "score":[1, 2, 3],
            "info":"This is additional information for question 1"
        }
    ,
    "1":{
            "question":"How many websites?",
            "answers":["One", "Two", "Three"],
            "score":[1, 2, 3],
            "info":"This is additional information for question 2"
        }
}
}

I use a class in which I have several functions to make array's which can be used on my regular .php page. I have made an array of questions using the following piece of code, which works:

$questions = [];

foreach($json['questions'] as $key => $value){
        $this->questions[] = $value['question'];
}

Now I can just use question[0] to get the first question and question[1] for the second, which is really nice. What I am trying to do is create an array that contains all the answers per question so I can do something similar with the answers. Ideally, it would look something like this:

array:
    arrayQuestion1Answers:
        string: answer1
        string: answer2
        string: answer3
    arrayQuestion2Answers:
        string: answer1
        string: answer2
        string: answer3

That way I could do something like arrayQuestion1[0] to get the first answer from the first question, and arrayQuestion2[2] to get the third answer from the second question.

Thank you for reading my long (and possibly stupid) question, hope you can help!

2
  • What's wrong with just working with the json-decoded array? "The first answer from the first question" would be $json['questions'][0]['answers'][0] Commented May 1, 2018 at 14:20
  • That is true, but I am wondering if it is possible to make an array of this. Commented May 1, 2018 at 14:25

2 Answers 2

2

Try this,

$json = '{
    "questions": {
        "0": {
            "question":"How many products?","answers":["Less than 1000", "More than 1000", "More than 10000"],"score":[1, 2, 3],"info":"This is additional information for question 1"
        }
        ,"1":{
            "question":"How many websites?","answers":["One", "Two", "Three"],"score":[1, 2, 3],"info":"This is additional information for question 2"
        }
    }
}
';
$questions = json_decode($json,true);
$question = $questions['questions'];
foreach($question as $key => $que):
    $answer['arrayQuestion'.$key.'Answers'] = $que['answers'];
endforeach;
echo '<pre>';
print_r($answer);
Sign up to request clarification or add additional context in comments.

Comments

-1

You say you already have functions to convert the JSOn to PHP arrays. Have you ever checked out json_decode()? It's a native PHP function to decode your JSON. It returns false on failure.

Furthermore: why are you indexing the objects in your questions object in your JSON? You might as well type:

{
    "questions": [
        {"somekey": "this_is_question_0"},
        {"somekey": "this_is_question_1"}
    ]
}

That way they're automatically indexed.

To answer you actual question, if you use what I describe above, you can simply access a question's answers like this:

$questions = json_decode($json);
$answers_to_question_1 = $questions[0]['answers'] ?? []; // ?? [] means: if not set, make it an empty array. This is the null coalesce feature, available in PHP 7.0 and higher. See http://php.net/manual/en/migration70.new-features.php.

2 Comments

that would be a great example, if your json was valid.
Ok mr. wiseguy, I know I can't type comments inside my JSON else I would've typed "invalid JSON, this is just an example", but I'll update the answer since you're technically right. Thanks for pointing it out for those who might not recognize invalid JSON, anyway.

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.