I think that code placed inside some function via include. There two scopes: inside function and outside function. But they are local both. You should add global $keyArray;
before $keyArray = array();
.
But it's better to use this array as function argument.