TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   array fundamentals (http://www.talkphp.com/absolute-beginners/4012-array-fundamentals.html)

allworknoplay 02-28-2009 09:43 PM

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

Wildhoney 03-01-2009 01:15 AM

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

Salathe 03-01-2009 01:44 AM

Quote:

Originally Posted by allworknoplay (Post 22027)
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); 


allworknoplay 03-01-2009 07:44 PM

Quote:

Originally Posted by Wildhoney (Post 22029)
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...

Salathe 03-01-2009 07:51 PM

Yes, the foreach loop does automatically reset the array before looping.

allworknoplay 03-01-2009 07:56 PM

Quote:

Originally Posted by Salathe (Post 22030)
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?

Salathe 03-01-2009 09:11 PM

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