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.

Guesser
  • 1,769
  • 3
  • 25
  • 52

3 Answers3

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.

floriank
  • 25,546
  • 9
  • 42
  • 66
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.

Jacob Mulquin
  • 3,458
  • 1
  • 19
  • 22
  • I don't know why it would be any easier to track down if there is a specific file for this kind of data. – Guesser Nov 13 '13 at 13:22
  • 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. – Jacob Mulquin Nov 13 '13 at 13:25
  • But what method, a constant doesn't work for an array and a variable is vulnerable to being changed. – Guesser Nov 13 '13 at 13:26
  • 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. – Jacob Mulquin Nov 13 '13 at 13:27
  • It's too open to being changed, it should be a constant. – Guesser Nov 13 '13 at 13:29
  • That's the point of a constant that they can't be changed even by mistake. – Guesser Nov 13 '13 at 13:32
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];
Legionar
  • 7,472
  • 2
  • 41
  • 70
  • I'm not sure why that is better that storing as a variable, it's as open to being changed inadvertently. – Guesser Nov 13 '13 at 13:27
  • 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. – Legionar Nov 13 '13 at 13:28
  • 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. – Guesser Nov 13 '13 at 13:31
  • And why it will be overwritten? You have it stored in `$_SESSION['my_small_data']`, so you will know, to not overwrite it... – Legionar Nov 13 '13 at 13:34
  • That makes sense for a small application with one developer yes. – Guesser Nov 13 '13 at 13:59