Amazon Suppose you have declared a global array, $siteMapEntries, and you have added values to it, and you would like to clear its values inside a local function so that when the function returns, that global array is really emptied.
The following will not work:
2 | $siteMapEntries = array(); |
Read on to see the solution.
Solution
The solution is to use unset() and the $GLOBALS array in the following way to clear $siteMapEntries values by reference:
unset($GLOBALS['siteMapEntries']);
The following PHP code illustrates how to empty a global array by reference:
03 | $siteMapEntries = array(); |
05 | function deleteSiteMapEntries(){ |
06 | unset($GLOBALS[ 'siteMapEntries' ]); |
07 | global $siteMapEntries; |
08 | $siteMapEntries = array(); |
11 | function addASiteMapEntry($loc, $priority){ |
12 | global $siteMapEntries; |
14 | $siteMapEntries[] = array( |
16 | 'priority' => $priority |
21 | echo "size before: " .count($siteMapEntries). "<br/>" ; |
22 | deleteSiteMapEntries(); |
23 | echo "size after: " .count($siteMapEntries). "<br/>" ; |
Questions? Let me know!