View Single Post
Old 02-28-2009, 09:43 PM   #1 (permalink)
allworknoplay
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default array fundamentals

Hey guys, I read the array article and I'm trying to shore up my fundamentals of arrays in PHP. I think I am having some confusion.

First, I assume that in PHP4 and PHP5, that arrays are passed by reference, just like objects in PHP5...is that correct?

Secondly, I am using PHP version 5.2.8


Code:
<?php




$test = array("house" => "car", "dog" => "cat");


###First Array Print
foreach($test as $key => $value) {
	
	echo "$key => $value \n";
	

}

$array_count = count($test);
echo "count: $array_count \n";


###Second Array Print
while(list($key,$value) = each($test)){
	
	echo "$key => $value \n";
}




?>
will print:

house => car
dog => cat
count: 2


But notice that the second array print doesn't print anything.

If I comment out the FIRST array print, then the second one will work and print this:

count: 2
house => car
dog => cat

Code:
<?php




$test = array("house" => "car", "dog" => "cat");


###First Array Print
#foreach($test as $key => $value) {
	
	#echo "$key => $value \n";
	

#}

$array_count = count($test);
echo "count: $array_count \n";


###Second Array Print
while(list($key,$value) = each($test)){
	
	echo "$key => $value \n";
}




?>


So I have 2 questions.


1) Why is it that simply printing out the array the first time suddenly makes the array inaccessible in another loop?

2) The fact that the second loop wasn't able to print anything when the first loop already printed something, why does my $array_count variable work? Why does it echo the number "2"??

shouldn't it be inaccessible just the same way the second loop wasn't able to print anything??
allworknoplay is offline  
Reply With Quote