NOTE: It seems I was wrong about what was happening, and that there is no issue using $a = array();. This is since all assignments to arrays is by copy. (I had thought there were some accesses by reference that were causing problems - but that was just a typo. I've added some details to an answer below.
I've got some PHP that looks like this:
$myArray = array();
function useArray() {
global $myArray;
// ... do something with myArray ...
}
function clearArray() {
global $myArray;
// ... Somehow clear the global array ...
}
I know this sucks from a design viewpoint, but it's required to work around some third-party code that I can't change...
My question is what can I put in the clearArray function to make it work?
The usual advice of using I guess I could loop over the keys in the array and unset each in turn - like this:$myArray=array(); or unset($myArray); don't work since they only change the local version, not the global version.
function clearArray() {
global $myArray;
foreach($key in array_keys($myArray) ) {
unset( $myArray[$key] );
}
}
But that seems hacky and unclear. Is there a better solution?
$myArray = []works -- do you get different results?$myArray = array();works fine - what issues are you having?