Jeffrey W
Jeffrey W

Reputation: 89

How to make a combined array of .json array's in PHP?

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!

Upvotes: 0

Views: 60

Answers (2)

minitauros
minitauros

Reputation: 2030

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.

Upvotes: -1

Anjali Patil
Anjali Patil

Reputation: 79

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);

Upvotes: 2

Related Questions