0

How will I loop the values of 'ques_title' ? Below is the code and its output, I even tried with looping the value i.e $i => $value but then no output is shown.

foreach($allquiz as $i) {
    echo "<pre>";
    print_r($i);
    echo "</pre>";
}

OUTPUT

Array
(
['ques_title'] => Ques 1
['ques_image'] => kitchenaid_mixer_ele_qNhwH.png
['choice_1'] => iphone7_rosegold.png
['choice_2'] => kitchenaid_mixer_ele_qNhwH.png
['choice'] => choice_2
['choice_3'] => iphone7_rosegold.png
['choice_4'] => iphone7_rosegold.png
['question_type'] => 1
)
Array
(
['ques_title'] => Q2
['ques_image'] => 
['choice_1'] => f
['choice_2'] => s
['choice_3'] => t
['choice_4'] => f
['question_type'] => 0
)

var_dump($allquiz);

OUTPUT:

array(2) {
 [0]=>
array(8) {
["'ques_title'"]=>
string(11) "qdea sdas d"
["'ques_image'"]=>
string(30) "kitchenaid_mixer_ele_qNhwH.png"
["'choice_1'"]=>
string(20) "iphone7_rosegold.png"
["'choice_2'"]=>
string(30) "kitchenaid_mixer_ele_qNhwH.png"
["'choice'"]=>
string(8) "choice_2"
["'choice_3'"]=>
string(20) "iphone7_rosegold.png"
["'choice_4'"]=>
string(20) "iphone7_rosegold.png"
["'question_type'"]=>
string(1) "1"
}
[1]=>
 array(7) {
 ["'ques_title'"]=>
string(10) "Q2sad asas"
["'ques_image'"]=>
string(0) ""
["'choice_1'"]=>
string(1) "f"
["'choice_2'"]=>
string(1) "s"
["'choice_3'"]=>
string(1) "t"
["'choice_4'"]=>
string(1) "f"
["'question_type'"]=>
string(1) "0"
}
}

I want to loop through all the variables and store them. How can I do it ?

1 Answer 1

1
foreach($allquiz as $i) {
    echo "<pre>";
    print_r($i["'ques_title'"]);
    echo "</pre>";
}
Sign up to request clarification or add additional context in comments.

6 Comments

Whats prints var_dump($allquiz); ?
In your array key name is 'ques_title' with ', not ques_title
try print_r($i["'ques_title'"]);
Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
Thank you, but may I know why is the key name inside ' with ' ?
|

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.