kellz Posted November 8, 2007 Share Posted November 8, 2007 hey.. I was reading a tutorial on arrays but i reached this part and they don't explain WHY.. I like to know WHY something does what it does.. so here it is^^ [color=red]// Append an item (note that the new key is 5, instead of 0 as you // might expect).[/color] $array[] = 6; print_r($array); I highlighted the part that I want explained (in red ofcourse lol) so, WHY does it start at 5? and not not 0? yesh it does look kinda 0'ish, atleast thats what I did expect.. edit: what?!? it didn't let me make it red!.. ok, just imagine it's red lol Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted November 8, 2007 Share Posted November 8, 2007 The code posted will make $array[0] contain the value '6'. If it actually has $array[5] contain '6' you are holding back some code Edit: Unfortunately you can't color text inside the codebox It would be a very nice feature though! Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted November 8, 2007 Share Posted November 8, 2007 What might have happened before the code you posted is that the previously array indexes (0, 1, 2, 3, 4) have already been used: <?php //Create an array containing 5 elements $array = array("1st Element", "2nd Element", "3rd Element", "4th Element", "5th Element"); //Another way of doing that $array[0] = "1st Element"; . . . $array[4] = "5th Element"; //Now if you add another element it will get the key 5: $array[] = "6th Element"; //This will become $array[5] since 0-4 has already been used ?> Quote Link to comment Share on other sites More sharing options...
kellz Posted November 8, 2007 Author Share Posted November 8, 2007 What might have happened before the code you posted is that the previously array indexes (0, 1, 2, 3, 4) have already been used: <?php //Create an array containing 5 elements $array = array("1st Element", "2nd Element", "3rd Element", "4th Element", "5th Element"); //Another way of doing that $array[0] = "1st Element"; . . . $array[4] = "5th Element"; //Now if you add another element it will get the key 5: $array[] = "6th Element"; //This will become $array[5] since 0-4 has already been used ?> kinda.. all elements in the array were unset, the original array was: $arr = array(1,2,3,4,5) and then they did a foreach loop and unset all the of the elements(?) but without deleting the array itself.. here is all of it: <?php // Create a simple array. $array = array(1, 2, 3, 4, 5); print_r($array); // Now delete every item, but leave the array itself intact: foreach ($array as $i => $value) { unset($array[$i]); } print_r($array); // Append an item (note that the new key is 5, instead of 0 as you // might expect). $array[] = 6; print_r($array); // Re-index: $array = array_values($array); $array[] = 7; print_r($array); ?> Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted November 8, 2007 Share Posted November 8, 2007 Ahh okay The explanation "without deleting the array it self" is a pretty good picture but of course not technically correct. I don't know how to explain it technically correct, but someone might. It has to do with the internal pointer of the array. It apparently does not jump back to index which has been made available by unset(). To get an idea of what the internal pointer is try look at current(), next(), prev() ect.: http://dk.php.net/manual/en/function.next.php (the example) Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted November 8, 2007 Share Posted November 8, 2007 I just did some reading up on unset() and the way the array behaves has nothing to do with the internal pointer of the array. Unset() simply destroys the value of an variable but the variable it self still exists. So if you unset($array[2]) then $array[2] will still exist but contain no value. So that is why the index is still used even though you have unset() it. Try this for the learning experience: <?php $var = 'I have a value'; //Will output "I have a value"; echo $var; unset($var); //Will output '$var is still set' if(isset($var)) { echo '$var is still set'; echo '<br>'; } else { echo '$var is not set'; echo '<br>'; } //Will output nothing echo $var; ?> Sorry for giving the wrong explanation at first Quote Link to comment Share on other sites More sharing options...
kellz Posted November 8, 2007 Author Share Posted November 8, 2007 I just did some reading up on unset() and the way the array behaves has nothing to do with the internal pointer of the array. Unset() simply destroys the value of an variable but the variable it self still exists. So if you unset($array[2]) then $array[2] will still exist but contain no value. So that is why the index is still used even though you have unset() it. Try this for the learning experience: <?php $var = 'I have a value'; //Will output "I have a value"; echo $var; unset($var); //Will output '$var is still set' if(isset($var)) { echo '$var is still set'; echo '<br>'; } else { echo '$var is not set'; echo '<br>'; } //Will output nothing echo $var; ?> Sorry for giving the wrong explanation at first yay it all made sense!!^^ lol learning something new feels so good! edit: I made this <?php $a = array( array("sean", "kelsey", "self"), array("marts", "fran", "mother") ); unset($a[1][2]); $a[1][2]="monkey"; echo $a[1][2]; ?> Quote Link to comment 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.