Having these questions and answers hard coded. I want to be able to read them from a text file and load the arrays questions with questions from questions.txt while also read answers array from answers.txt. but at the same time maintain my data structures used. How should I go about this?

<?php
$questions = array('FTP','AJAX','RSS','XSS','PHP','W3C','XML','YUI','HTML','CGI','SSL','SQL','HTTP','CSS','SOAP','WAI','SSI','JSON','XSLT','WCAG');
$answers = array(
    // the correct answer for each question must be the first array item 0=>'item'
array(0 =>'File Transfer Protocol','Force Through Privately','File Through Protocol','File Test Protocol'),
array(0 => 'Asynchronous JavaScript and XML','All JavaScript and XML','Alternative Java and XML','Actual JavaScript and XML'),
array(0 => 'Really Simple Syndication','Really Simple Scripting','Ready-Styled Scripting','Really Stupid Syndication'),
array(0 => 'Cross-site Scripting','Cross-site Security','Cleverly Structured Scripting','eXtremely Safe and Secure'),
array(0 => 'PHP: Hypertext Preprocessor','Post Hypertext Processor','Practical HTML Processing','Process HTML Prettily'),
array(0 => 'World Wide Web Consortium','World Wide Web Committee','World Wide Web Creatives','Wakefield Willy Wavers Club'),
array(0 => 'eXtensible Markup Language','eXtendable Markup Language','Crossover Markup Language','eXtreme Markup Language'),
array(0 => 'Yahoo User Interface','Yahoo\'s Useful Idea','Yahoo Utility Interface','Yahoo User Interaction'),
array(0 => 'Hypertext Markup Language','Human Markup Language','Helpful Markup Language','Hypertext Memory Language'),
array(0 => 'Common Gateway Interface','Common or Garden Interaction','Computer\'s Graphical Intelligence','Common Graphical Interface'),
array(0 => 'Secure Sockets Layer','Server Security Layer','Server Security Level','Secret Socket Layer'),
array(0 => 'Structured Query Language','Stupid Query Language','Secure Query Language','Strict Query Language'),
array(0 => 'Hypertext Transfer Protocol','Hypertext Traffic Protocol','HTML Traffic Transfer Protocol','HTML Through Traffic Protocol'),
array(0 => 'Cascading Style Sheets','Custom Style Sheets','Clientside Style Sheets','Calculated Style Sheets'),
array(0 => 'Simple Object Access Protocol','Structured Object Access Protocol','Simple, Objective And Private','Simply Obvious Access Principle'),
array(0 => 'Web Accessibility Initiative','World Wide Accessibility Intiative','World Wide Accessibility Incorporation','Web Accessibility and Inclusion'),
array(0 => 'Server-Side Include','Server-Side Intelligence','Scripted Server Include','Secure Server Include'),
array(0 => 'JavaScript Object Notation','JQuery-Scripting Object Notation','Just Simple Object Notation','JavaScript Over the Net'),
array(0 => 'eXtensible Stylesheet Language Transformation','eXpandable Stylesheet Language Transfer','eXtensible Stylesheet Language Transfer','eXtendable Stylesheet Language Transformation'),
array(0 => 'Web Content Accessibility Guidelines','Wakefield Community Action Group','Web Criteria And Guidelines','World-wide Common Access Group')
); 
?>

Recommended Answers

All 2 Replies

You could use file_put_contents() to save the array to the text files:

file_put_contents('questions.txt', json_encode($questions));

Then use file_get_contents() to get the arrays from the files and use array_combine() to match them. As suggestion use json_encode() to save data to txt files, and then just run this to retrieve:

$questions = json_decode(file_get_contents('questions.txt'),TRUE);

For more information check the manual:

I assume that the number of question equal to number of answesr
You can do this way save your question and answer in txt or csv file and keep it tab seprated.
Like
FTP<tab>File Transfer Protocol<tab>Force Through Privately<tab>File Through Protocol<tab>File Test Protocol

You can use any seprator like " | " or "~" etc. Make sure that this seprator is not present in any of your text.

FTP|File Transfer Protocol|Force Through Privately|File Through Protocol|File Test Protocol

After the file saving write a code

$handle1=fopen("QuestionAnswer.txt","r");
while(($data=fgetcsv($handle1,2500,"\t"))!=false)
{
    $question   =   trim($data[0]);
    $option1    =   trim($data[1]);
    $option2    =   trim($data[2]);
    $option3    =   trim($data[3]);
    $option4    =   trim($data[4]);
}

This way you can maintain your txt or csv file and you can add more columns to the file and read.
For eg. Question Id or Right answer column

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.