0

Instead of going to the database for what would be a very small table, e.g 3 columns and 3 rows. I want to put that information in an array or JSON object and have it be included in the applications header file. The information is used often enough to warrant it's inclusion on every page.

So it seems I cannot save the array as a Constant, so what would be the best approach to access the array at anytime, preferably with no further processing. It would be good to store it as a JSON object as the info will be used with Javascript as well as PHP.

4
  • Do you mean u have an array of constant data? Commented Nov 13, 2013 at 13:15
  • Yes the data will not change Commented Nov 13, 2013 at 13:19
  • Negative votes from the trolls lol Commented Nov 13, 2013 at 13:21
  • Check this out: stackoverflow.com/questions/1290318/… Hope it helps! Commented Nov 13, 2013 at 13:41

3 Answers 3

2

If you want something that can't be changed try this. You'll just have to load the class before you can use it in your applications bootstrap process or where ever you do your class loading or register it in your class loader.

final class SomeData {
    private static $data = array(
        1 => array(
            1 => '1st row 1st column',
            2 => '1st row 2nd column',
            3 => '1st row 3rd column'
        ),
        2 => array(
            1 => '2nd row 1st column',
            2 => '2nd row 2nd column',
            3 => '2nd row 3rd column'
        ),
        3 => array(
            1 => '3rd row 1st column',
            2 => '3rd row 2nd column',
            3 => '3rd row 3rd column'
        )
    );
    public static function getData() {
        return self::$data;
    }
}

print_r(SomeData::getData());

If you want to access the data object like an array you could simply implement ArrayAccess . The object can't be modified because it's final and the property private.

Sign up to request clarification or add additional context in comments.

3 Comments

This is working well, is there no way to access the array without the use of a function?
No, you can't change it.
@burzum - well, you could change it if you used Reflection, but not in regular usage. In any case, it's as close as you're going to get to a constant array.
0

An array would be useful if the information does not need to be modified, as to change it would require manually editing the source code.

You could store information in a .json file, and on each session load the values into an array.

To be honest it would be best to go with SQLite or similar. Even MySQL would be appropriate, not to mention it saves a lot of hassle down the track if data needs to be modified.

6 Comments

I don't know why it would be any easier to track down if there is a specific file for this kind of data.
I noticed in the other comment you said that the data would not change, in this case it would perfectly fine to use arrays to store the information.
But what method, a constant doesn't work for an array and a variable is vulnerable to being changed.
A PHP Variable: $db['table1']['row1']['field1'] = 't1,r1,f1'; $db['table1']['row1']['field2'] = 't1,r2,f2'; $db['table1']['row1']['field3'] = 't1,r1,f3'; $db['table2']['row3']['field4'] = 't2,r3,f4'; Or something similar. PHP Variables are not vulnerable change, only if you change them. You can then dump this array as a json encoded string for use by Javascript.
It's too open to being changed, it should be a constant.
|
0

So then store it as an array in the $_SESSION. And it will be accessible anytime.

This code will be in your header:

session_start();

$_SESSION['my_small_data'] = array(
  1 => array(
    1 => '1st row 1st column',
    2 => '1st row 2nd column',
    3 => '1st row 3rd column'
  ),
  2 => array(
    1 => '2nd row 1st column',
    2 => '2nd row 2nd column',
    3 => '2nd row 3rd column'
  ),
  3 => array(
    1 => '3rd row 1st column',
    2 => '3rd row 2nd column',
    3 => '3rd row 3rd column'
  )
);

And everywhere in your code you can access f.e. second row third column with this code:

$data = $_SESSION['my_small_data'][2][3];

5 Comments

I'm not sure why that is better that storing as a variable, it's as open to being changed inadvertently.
But you want to have it accessible everywhere, so also in other PHP files in your project; and I guess you are not using globals on... Also then if you are using OOP, you will need to have it accessible in your classes... So then using $_SESSION is your best choice.
If it's accessed via the header in a variable or put in a session variable that makes no difference. It needs to be a constant to avoid being overwritten.
And why it will be overwritten? You have it stored in $_SESSION['my_small_data'], so you will know, to not overwrite it...
That makes sense for a small application with one developer yes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.