cliftonbazaar Posted May 22, 2009 Share Posted May 22, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/159198-having-an-array-that-everyone-uses/ Share on other sites More sharing options...
MadTechie Posted May 22, 2009 Share Posted May 22, 2009 whats wrong with it as it stands ? if you need the array then your need it, but is it needed on everypage ? Quote Link to comment https://forums.phpfreaks.com/topic/159198-having-an-array-that-everyone-uses/#findComment-839576 Share on other sites More sharing options...
cliftonbazaar Posted May 22, 2009 Author Share Posted May 22, 2009 The problem is that every user has their own copy even though they are all excactly the same, surely this would take up extra server processes? Quote Link to comment https://forums.phpfreaks.com/topic/159198-having-an-array-that-everyone-uses/#findComment-839595 Share on other sites More sharing options...
Ken2k7 Posted May 22, 2009 Share Posted May 22, 2009 What do you mean every user has their own copy? Are you creating a new file with that array for each user? Why not utilize include or require? Quote Link to comment https://forums.phpfreaks.com/topic/159198-having-an-array-that-everyone-uses/#findComment-839642 Share on other sites More sharing options...
alphanumetrix Posted May 22, 2009 Share Posted May 22, 2009 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" Quote Link to comment https://forums.phpfreaks.com/topic/159198-having-an-array-that-everyone-uses/#findComment-839658 Share on other sites More sharing options...
cliftonbazaar Posted May 22, 2009 Author Share Posted May 22, 2009 What do you mean every user has their own copy? Are you creating a new file with that array for each user? Why not utilize include or require? Reading up on them now. Quote Link to comment https://forums.phpfreaks.com/topic/159198-having-an-array-that-everyone-uses/#findComment-839660 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.