TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-28-2009, 09:43 PM   #1 (permalink)
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
Old 03-01-2009, 01:15 AM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

This is because within the foreach loop you have moved the array pointer one place each time. After the foreach loop the pointer points to the very end of the array, and current will return NULL. However, if you reset the pointer of the array in between your loops as seen below, your second loop will loop correctly because the array pointer has been reset to the beginning.

This works like so because your second loop is not a foreach loop. A foreach automatically resets the array pointer before looping again.

php Code:
echo 'Current Index = ' . current($test) . "\n";
reset($test);
echo 'Current Index = ' . current($test) . "\n";
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
allworknoplay (03-01-2009)
Old 03-01-2009, 01:44 AM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by allworknoplay View Post
I assume that in PHP4 and PHP5, that arrays are passed by reference, just like objects in PHP5...is that correct?
No, arrays are passed by value unless explicitly passed by reference (the same way as objects were handled in PHP4). Example:
PHP Code:
$one   =  array('a' => 'apple''b' => 'banana''c' => 'car');
$two   =  $one;
$three =& $one;

$one['b'] = 'ball';

var_dump($one$two$three); 
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
allworknoplay (03-01-2009)
Old 03-01-2009, 07:44 PM   #4 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
This is because within the foreach loop you have moved the array pointer one place each time. After the foreach loop the pointer points to the very end of the array, and current will return NULL. However, if you reset the pointer of the array in between your loops as seen below, your second loop will loop correctly because the array pointer has been reset to the beginning.

This works like so because your second loop is not a foreach loop. A foreach automatically resets the array pointer before looping again.

php Code:
echo 'Current Index = ' . current($test) . "\n";
reset($test);
echo 'Current Index = ' . current($test) . "\n";

Thanks WH:

Does the foreach function automatically reset the array pointer to the beginning? Because when I run the same loop again, I actually get an output like so:

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";


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

}




?>

So I get this output:

house => car
dog => cat
count: 2
house => car
dog => cat

Whereas the WHILE loop doesn't reset the pointer position which would explain why in the original code, it didn't output anything...
allworknoplay is offline  
Reply With Quote
Old 03-01-2009, 07:51 PM   #5 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Yes, the foreach loop does automatically reset the array before looping.
Salathe is offline  
Reply With Quote
Old 03-01-2009, 07:56 PM   #6 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
No, arrays are passed by value unless explicitly passed by reference (the same way as objects were handled in PHP4). Example:
PHP Code:
$one   =  array('a' => 'apple''b' => 'banana''c' => 'car');
$two   =  $one;
$three =& $one;

$one['b'] = 'ball';

var_dump($one$two$three); 
Thanks Sal:

I got this output and I understand it:

array(3) {
["a"]=>
string(5) "apple"
["b"]=>
string(4) "ball"
["c"]=>
string(3) "car"
}
array(3) {
["a"]=>
string(5) "apple"
["b"]=>
string(6) "banana"
["c"]=>
string(3) "car"
}
array(3) {
["a"]=>
string(5) "apple"
["b"]=>
string(4) "ball"
["c"]=>
string(3) "car"
}


I thought it was pass by value until the other day I wrote a quick script that used array_pop and I got confusing results.
So I thought maybe it might actually be pass by reference...

But I just wrote another script and now it works as advertised. I wish I could remember the other script so I can see where it went wrong.


Quick question about proper syntax. Both of these below worked, and I'm sure it's just preference but what do you think is the best syntax to use?


$three =& $one;

$three = &$one;

The only reason why I would say the second example is that when you are passing by reference to a function it would look more like the second one:

test_function(&$variable)


thoughts?
allworknoplay is offline  
Reply With Quote
Old 03-01-2009, 09:11 PM   #7 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Some functions, like array_pop as you mentioned, pass some arguments by reference. Examples include array_pop and array_walk whose first arguments (the array to work on) are passed by reference.

As for =& $var versus = &$var, that's up to personal preference. Me, I like to think of it as "assign by reference" not "assign a reference" so would go with =& usually.
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How Would I Apply htmlentities() To Every Array Item vujsa Advanced PHP Programming 11 01-10-2009 01:28 PM
Array mess Killswitch Absolute Beginners 4 12-14-2008 07:35 AM
Build multi-dimensional array out of a flat array drewbee Advanced PHP Programming 2 05-28-2008 11:38 PM
(array) or array()? Orc General 4 01-20-2008 07:52 PM
Part 1: Getting Started with Array Functions Wildhoney Absolute Beginners 6 10-01-2007 10:53 AM


All times are GMT. The time now is 03:27 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design