-4

I have an array that I need in my Controller:

$myArray = array('key'=>'value', key1'=>'value1', 
                  key'=>'value', key'=>'value'key'=>'value'....);

It's not good to keep it like this.

Do you know where can I "store" and call it when its needed?

Shawn
  • 3,583
  • 8
  • 46
  • 63
Smaïne
  • 1,374
  • 12
  • 19
  • 2
    why is it not good to keep it like that ? what are you doing with this array ? – t-n-y Jan 12 '17 at 14:12
  • refer to this post http://stackoverflow.com/q/38602921/6521116 – LF00 Jan 12 '17 at 14:12
  • What's issue with this method? – Gopal Joshi Jan 12 '17 at 14:22
  • I think it's a bad practice because I have more 25 items... – Smaïne Jan 12 '17 at 15:15
  • I think it's a bad practice because I have more 25 items... With this array I access to my entity and show it nicely. for exemple I have an entity and I want to convert the column name. For example: entity.champ will become myArray[entity.name] – Smaïne Jan 12 '17 at 16:02
  • Did you know that since this is an associate array, you can't have duplicate key values. For example, you can't use `'key1' => 'value', 'key1' => 'value2'`. The reason being if you try to access `$myArray['key1']` it wouldn't know which key to access. – Alvin Bunk Jan 12 '17 at 16:48
  • 1
    I would suggest to put that array in a yml file and parse that file in your controller – Emanuel Oster Jan 12 '17 at 17:08

1 Answers1

0

You can store the array in YAML file. Put the array into Resources/config/my_array.yml (or another file name) as follows:

key: "value"
key1: "value1"
...

In a controller you can get the array like this:

$locator = new \Symfony\Component\Config\FileLocator($this->getParameter('kernel.root_dir') . '/../src/AppBundle/Resources/config');
$parser = new \Symfony\Component\Yaml\Parser();
$myArray = $parser->parse(file_get_contents($locator->locate('my_array.yml'));
Const Se
  • 23
  • 4