Presetting arrays - best method?

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Presetting arrays - best method?

Post 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?
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Presetting arrays - best method?

Post 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.
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Presetting arrays - best method?

Post 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';
 
Fighting for peace is declaring war on war. If you want peace be peaceful.
Post Reply

Return to “Beginner Help and Support”