Page 1 of 1
Presetting arrays - best method?
Posted: Tue Aug 02, 2011 1:06 am
by Callan S.
I'm going to code something soon where for gameplay, there are a set of questions and a set of answers and it's a memory game where, when presented with a question, you have to remember the right answer and click it.
I'm intending to just put a long preset array in the php file, like
Code: Select all
$question[1]="blah blah";
$question[2]="guh guh";
There wont be a huge number - starting with nine questions and nine answers. It might go up in future, but will probably never breach one hundred of each.
Is there a better method? Or is the above handled so fast I may as well not bother thinking about it?
Re: Presetting arrays - best method?
Posted: Tue Aug 02, 2011 1:12 am
by Xaleph
$questions = array(
0 => array('question' => 'blah blah', 'awnser' => 'boo boo'),
1 => array('question' => 'blah blah', 'awnser' => 'boo boo'),
2 => array('question' => 'blah blah', 'awnser' => 'boo boo'),
3 => array('question' => 'blah blah', 'awnser' => 'boo boo'),
4 => array('question' => 'blah blah', 'awnser' => 'boo boo'),
5 => array('question' => 'blah blah', 'awnser' => 'boo boo'),
6 => array('question' => 'blah blah', 'awnser' => 'boo boo'),
7 => array('question' => 'blah blah', 'awnser' => 'boo boo'),
8 => array('question' => 'blah blah', 'awnser' => 'boo boo'),
9 => array('question' => 'blah blah', 'awnser' => 'boo boo'),
);
This is what I would do. Memory usage is not something you should worry about. Unless you want 10k + records.
Re: Presetting arrays - best method?
Posted: Thu Sep 08, 2011 7:39 pm
by Chris
The above is processed so fast we can't even fathom the time it was done in. What slows stuff down is the stuff between the quotation marks, it all has to be kept in storage.
Writing an array is all really down to personal preference and what makes your code look neater in the situation, in PHP there are two ways of building an array:
Code: Select all
$array = array( 'key' => 'value' );
// or
$array = array();
$array['key'] = 'value';