Grayda Posted November 22, 2007 Share Posted November 22, 2007 As a part of my job, I'm required to build database-driven applications for collecting and maintaining hardware. So far things have been going well. Until I met unset() and loops. Take this sample code: <?php $arr = array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => ; foreach($arr as $val) { unset($val); } echo var_dump($arr); ?> Looking over it, it seems pretty easy. 8 items in an array. the foreach loop goes through the array, unsetting each item. What should we expect to see? To me, I expect to see a blank array. But instead, $arr remains untouched and var_dumping $arr produces: array( { [1]=> int(1) [2]=> int(2) [3]=> int(3) [4]=> int(4) [5]=> int(5) [6]=> int(6) [7]=> int(7) [8]=> int( } Am I missing something here? This happens with any sort of loop I try (foreach, for, do etc.) and with any sort of data I throw at it. The loop runs 8 times (stick an $i++; and an echo and watch it go), but refuses to unset the variable. I doubt this is a scope issue as there are no functions / classes / whatever to get in the way. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/78362-unset-exits-loop-php-version-523/ Share on other sites More sharing options...
teng84 Posted November 22, 2007 Share Posted November 22, 2007 <?php $arr = array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => ; foreach($arr as $val) { unset($val); } echo var_dump($arr); foreach($arr as $key=> $val) { unset($arr[$key]); } echo '<br> see the chnages here <br>'; echo var_dump($arr); ?> i will not explain just see the sample i gave you ... Quote Link to comment https://forums.phpfreaks.com/topic/78362-unset-exits-loop-php-version-523/#findComment-396498 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.