Welcome Guest, Not a member yet? Register   Sign In
Where store a lot of arrays?
#1

(This post was last modified: 11-12-2017, 06:54 AM by emilovbg.)

I have some collection of static arrays, that I need to use somewhere in my app - that are hundreds names, families, countries, that I`m using to generate some random data, or display in a view, or use in model for other needs. I`m not sure where to store these arrays. First I make a custom config file and put them there, but I don`t think is it the right place. Now, I`m using library class, but Im not sure that i propose of libraries.


PHP Code:
class Storage {

private 
$someArray = [...];

private 
$anotherArray = [...];

private 
$thirdArray = [...];

public function 
getArray($type
{
   $array $this->{$type};
   return $array;
}
 

Reply
#2

(This post was last modified: 11-12-2017, 07:00 AM by PaulD.)

Why not store them in a database and call them as needed?

If you need say 10 random names from your hundreds of names, just do a query like the ones described here:
https://stackoverflow.com/questions/4329...-rows-fast
Reply
#3

Becouse is a static data that do no need to be in database. I have array that hold all my tactics, I use it in view - to make option menu, I used in my Engine to grab some data related to each tactic, I used in controller to inject the models. This is like a config data, but a bit more...
Reply
#4

Then in my opinion both methods you have used are fine. I think I would use libraries and config files. In your library load the config files you need for each function (unless you always need all of them all the time) then creating added functionality like getting a single name, family and country for a profile, you can easily build it in. Having the data in separate config files is clean too, keeping the large static arrays out of the libraries.
Reply
#5

Both. Use separate configuration files to put your data and then create a library (it could be a model too, $this conveniently behaves there in a similar way as in controllers) that is to read the configuration files within their methods or, if necessary within the constructor. While reading this specific configuration use the "section" parameter for not making a possible conflict with the existing common configuration. Then in your application use only the library (or the model), it will "know" where to read data from, writing code would be easy.
Reply
#6

if I have static data i usually keep it under config dir, but in your case if you have a large set then you might as well create a directory under application called data and put it there.
Reply
#7

(This post was last modified: 12-27-2017, 07:07 AM by XtreemDeveloper.)

You can manage these array values by database. If you are use database for store these array than you can easily call tham as needed.

Also you can handle these arrays by helper.

You can create a function in helper file

function handleArrayValues($array_type) {
//helper file code
if($array_type == "someArray") {
$someArray = [...];
return $someArray;
} else if($array_type == "anotherArray") {
$anotherArray = [...];
return $anotherArray;
} else if($array_type == "thirdArray") {
$thirdArray = [...];
return $thirdArray;
} else {
return false;
}
}


//get arary as you want (where you want)
handleArrayValues("anotherArray");


Second option you can create these array in model and you can load that model in autoload file for access where in project.

//Model code
private $someArray = [...];

private $anotherArray = [...];

private $thirdArray = [...];

function getArray($type) 
{
   $array = $this->{$type};
   return $array;
}

//get arary as you want (where you want)
$this->model_name->getArray($thirdArray);
Reply




Theme © iAndrew 2016 - Forum software by © MyBB