Jump to content

Recommended Posts

I wish to have an array that EVERYONE uses (I have thought of other methods but this seems to be the best).

 

At the moment I have a setup page for every user so that when they start to play the array is set up

//We have a new play session so let's set up this particular array
$building_array= array("castle", "storage", "barracks", "hospital", "tower", "inn");  //This is just a small part of the array, it is a lot bigger 

 

then I realised that this array rarely changes (and when it does it is the same for everyone).  So every user has this array in memory(?), is there a way of coding an array so that every one uses it but takes up a lot less memory?

Link to comment
https://forums.phpfreaks.com/topic/159198-having-an-array-that-everyone-uses/
Share on other sites

Are you running your site through an index (single file)?

 

If so, just put the array in your main script, and all your files can have access to it.

 

If you mean that you are creating an array for every user, a good idea would be to create a multi-dimensional array instead.

 

IE of multi-dimensional array:

$building_array = array(

'user1' => array('castle', 'storage', 'barracks', 'hospital', 'tower', 'inn'),
'user2' => array(1, 2, 3, 4, 5, 6)

);

 

You can then access the data like this:

 


echo $building_array['user1'][0]; // will display "castle"

echo $building_array['user2'][0]; // will display "1"

 

You can make the arrays more detailed by changing it like so:

 


$building_array = array(

'user1' => array( 'castle' => 'some castle name', 'storage' => 'some storage name' )

);


echo $building_array['user1']['castle']; // will display "some castle name"

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.