PHP   

Multi dimension PHP array to hold multiple choice questions

This readymade PHP code snippet can be used for storing multiple choice questions in multi dimension PHP array

<?php
$awesomeArray = array(
	
			array(
			"question"=> "Question 1 goes here?",	
			"answers" => array(
							array("answer"=>"Q1 Answer 1"), 
							array("answer"=>"Q1 Answer 2"),
							array("answer"=>"Q1 Answer 3"),
							array("answer"=>"Q1 Answer 4"),
						),
			),
			array(
			"question"=> "Question 2 goes here?",	
			"answers" => array(
							array("answer"=>"Q2 Answer 1"), 
							array("answer"=>"Q2 Answer 2"),
							array("answer"=>"Q2 Answer 3"),
						),
			),
			array(
			"question"=> "Question 3 goes here?",	
			"answers" => array(
							array("answer"=>"Q3 Answer 1"), 
							array("answer"=>"Q3 Answer 2"),
						),
			),
			array(
			"question"=> "Question 4 goes here?",
			"grid"	=>	"2x2",			
			"ques_class"	=>	"ques_with_images",			
			"answers" => array(
							array("answer"=>"Q4 Answer 1", "image"=>"Q4_A1_img.jpg"), 
							array("answer"=>"Q4 Answer 2", "image"=>"Q4_A2_img.jpg"),
							array("answer"=>"Q4 Answer 3", "image"=>"Q4_A3_img.jpg"),
							array("answer"=>"Q4 Answer 4", "image"=>"Q4_A3_img.jpg"),
						),
			),
			array(
			"question"=> "Question 5 goes here?",	
			"answers" => array(
							array("answer"=>"Q4 Answer 1"), 
							array("answer"=>"Q4 Answer 2"),
						),
			),
		
	);

	$i=1; 
	$question_count = sizeof($awesomeArray);
	foreach($awesomeArray as $question){
		echo $question['question']."<br>"; 
		foreach($question['answers'] as $answer){
			if(isset($answer['image'])): 
				echo $image=$answer['image']."<br>";
			endif;
			echo $answer['answer']."<br>";	
		}
		$i++;
	}
?>
Need Professional Support for Your Website Problems?

Whether you're facing website issues or struggling with code implementation, our team is here to assist. Hire us to get your website back on track.

Hire Us