Initialising the entire array (all keys, sub arrays etc) somewhere first is not practical.
This means remembering and maintaining it - when you have new data not previously accounted for, you have to also add it to the array initialisation.
(Urgh)
I would least delcare the var as an array ($data = array();) then you don't need is_array() - which is an annoying single line of code before you even do anything useful (two lines including the closing brace..).
However, your checking is not needed.
Your code checks if the array's sub array has been set, and if not you set it as an array and then set the data onto it.
This is not necessary, as you can set data in an array even if some of the sub keys/arrays had not previously been set.
For example this code (this is the entire file and all code ran) won't throw an error, warning, or notice:
$data['a']['one']['blah']['foo'] = 'test';
print_r($data);
echo $data['a']['one']['blah']['foo'];
The above outputs:
Array ([a] => Array ( [one] => Array ( [blah] => Array ( [foo] => test ) ) ) )
test
The above code will not return any warning/notice/errors.
Also notice I didn't even have $data = array().
(although you want to, as you likely use foreach and that initialisation negates the need for using is_array).
That was with error_reporting(-1);
TL;DR; - The Answer
So to answer your question, in your code you could do this (and receive no errors/notices/warnings):
$data = array();
// more data setting the array up
foreach ($items as $item) {
$data['a']['one'] = $item->getId();
}
@before$data['a']['one'] = 'test';but it's stupid and can cause many problems in future,@will ignore errors/warning/notices... etc