parsing multidimensional js object in php

Wednesday, November 4, 2015

I have a js object that looks like this:



0: Object
answer: Object
answer1: "fweq"
answer2: "qwef"
answer3: "fqwef"
answer4: "vrevwe"
question: Object
content: "<p>my content</p>"
title: "test"
type: Object
type: "addmcq"



I am trying to iterate and parse this in php.
However its messing with my head.



My php looks like this:



$question = $_POST['data'];
$question = json_decode($question, true);
var_dump($question);
foreach($question as $q) {
foreach($q as $item) {
$type = $item['type']; //Echoes the type correctly
echo $item['content']; //Does nothing
echo $item['answer1']; // Does nothing
echo $item['answer2']; // --
echo $item['answer3']; // --
echo $item['answer4']; // --

}
}


$type will echo out whatever is in the type object but $item['answer1'] will not.



How can i iterate the answer array?



heres the output of the var_dump(question):



array(1) {
[0]=>
array(3) {
["question"]=>
array(2) {
["title"]=>
string(48) "
test
"
["content"]=>
string(12) "<p>asetw</p>"
}
["answer"]=>
array(4) {
["answer1"]=>
string(4) "fweq"
["answer2"]=>
string(4) "qwef"
["answer3"]=>
string(5) "fqwef"
["answer4"]=>
string(6) "vrevwe"
}
["type"]=>
array(1) {
["type"]=>
string(6) "addmcq"
}
}
}

0 comments:

Post a Comment