Jump to content

from array tutorial


kellz

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/76555-from-array-tutorial/
Share on other sites

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


?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/76555-from-array-tutorial/#findComment-387729
Share on other sites

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);
?> 

Link to comment
https://forums.phpfreaks.com/topic/76555-from-array-tutorial/#findComment-387730
Share on other sites

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)

Link to comment
https://forums.phpfreaks.com/topic/76555-from-array-tutorial/#findComment-387736
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/76555-from-array-tutorial/#findComment-387740
Share on other sites

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

<?php
$a = array(
           array("sean", "kelsey", "self"),
	   array("marts", "fran", "mother")
	  );
	  
unset($a[1][2]);
$a[1][2]="monkey";
echo $a[1][2];
?>

Link to comment
https://forums.phpfreaks.com/topic/76555-from-array-tutorial/#findComment-387779
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.