Jump to content

Recommended Posts

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 :)

Link to comment
https://forums.phpfreaks.com/topic/78362-unset-exits-loop-php-version-523/
Share on other sites

<?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 ...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.