user1000232
user1000232

Reputation: 219

Dealing with large arrays in PHP

Okay a couple of simple questions.

First, am I correct to assume that doing this is a bad idea because the array gets recreated each time the function is called?

function foo(){
    $arr = {"REALLY_BIG_ARRAY":"HAS LIKE 1000 ELEMENTS"}; 
}

Now to deal with this I came up with the idea of doing this:

class example {
    public static $property =  {"REALLY_BIG_ARRAY":"HAS LIKE 1000 ELEMENTS"};
}

function foo(){
    //to use the array I do
    foo::$property["some_element"]; //Do something with this 
}

I am using this in a small web app I am currently building. Are there any good ways of dealing with big data arrays in PHP. This function gets called a lot so thats why putting the array in DV seemed like a bad idea.

Upvotes: 0

Views: 412

Answers (1)

Barmar
Barmar

Reputation: 780818

You can use a static variable:

function foo(){
    static $arr = {"REALLY_BIG_ARRAY":"HAS LIKE 1000 ELEMENTS"}; 
}

It will only be created the first time you call the function, future calls will reuse the value.

Upvotes: 1

Related Questions