In order to keep things nice and clean and in this case, simple, you might be better off using array_merge( )
I personally declare any arrays at the top of my class file, in order to make them globally accessible, only because I tend to keep methods free of array declaration (OCD I guess!)
So for me I have an example that might help you, it's something that works for me when needed to add/merge two arrays together:
protected $array1 = array (
'basic' => '1',
'example' => '2',
'for' => '3'
);
protected $array2 = array(
'merging' => '4',
'two' => '5',
'associative' => '6',
'arrays' => '7',
'mate' => '8'
);
Then within your class file, you can use these arrays or any created arrays and merge whenever you want:
public function ExampleOne()
{
$firstArray = $this->array1;
print_r($firstArray);
$secondArray = $this->array2;
print_r($secondArray);
$merged = array_merge($firstArray, $secondArray);
print_r($merged);
}
Each print_r( ) will give you a print out in the console of the data/created array. This is so you can view for yourself that everything has been created correctly and each key has its associated value (check the PHP man pages for a definitive explanation of print_r( ) ).
So, the first array will/should showcase this:
Array
(
[basic] => 1
[example] => 2
[for] => 3
)
The second array will/should showcase this:
Array
(
[merging] => 4
[two] => 5
[associative] => 6
[arrays] => 7
[mate] => 8
)
And the array_merge( ) operation will create the final array, which will/should showcase this:
Array
(
[basic] => 1
[example] => 2
[for] => 3
[merging] => 4
[two] => 5
[associative] => 6
[arrays] => 7
[mate] => 8
)
Of course, you don't always have to place/create your arrays at the top of the class file and when needed you can obviously create arrays within a single function if they are only needed/used within there - what I showcased here was just something I had done recently for a project at work (with the data in these arrays being example data of course!)