Forum Moderators: coopster

Message Too Old, No Replies

problem unsetting array

         

jackvull

11:40 am on Jun 5, 2006 (gmt 0)

10+ Year Member



Hi
I have this code inside a function:

global $arrProductData, $arrItemData;

foreach ($arrProductData as $index => $value) {
unset($arrProductData[$index]);
}
foreach ($arrItemData as $index => $value) {
unset($arrItemData[$index]);
}
reset($arrProductData);
reset($arrItemData);

I am trying to:
- loop through some XML files
- fill up the array with data from 1 file
- insert the data into a database
- reset the array (as above)
- pick up the next XML file
- fill the aray with data again, etc.

It seems that when I do the above, the array is not being destroyed, as the data is all being duplicated.
Is there a better way of destroying the arrays, so that they can be filled with new values?

Sekka

11:54 am on Jun 5, 2006 (gmt 0)

10+ Year Member



Can't you just do,

unset ($arrProductData);
unset ($arrItemData);

?

I may be very wrong, that's just what I'd do.

jackvull

1:05 pm on Jun 5, 2006 (gmt 0)

10+ Year Member



apparently not. I would have thought that would have been the easiest way, but you have to wipe each individual key.

To make it worse, it only unsets that particular key but doesn't destroy the array entirely so that if you do this:
$array[0] = 0;
$array[1] = 1;

unset them then do

isset($array[1])

it will still bring back true instead of false.

So, there must be a way of getting rid of the array completely?

coopster

2:56 pm on Jun 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You are bumping into variable scope here. You are globalizing the variable inside your function and if a globalized variable is unset() [php.net] inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called. If you would like to unset() a global variable inside of a function, you can use the $GLOBALS array to do so:
unset($GLOBALS['$arrProductData']); 
unset($GLOBALS['$arrItemData']);