![]() |
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:
<?phphouse => 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:
<?phpSo 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?? |
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. |
Quote:
PHP Code:
|
Quote:
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:
<?phpSo 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... |
Yes, the foreach loop does automatically reset the array before looping.
|
Quote:
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? |
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. |
| All times are GMT. The time now is 06:29 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0