Re: iterating and changing value of an array using foreach and references - PHP 5.2.3

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 8/18/07, Yashesh Bhatia <yasheshb@xxxxxxxxx> wrote:
> Hi,
>
>   Here's an interesting observation i noticed while using foreach to
> iterate on arrays using
> references for it's values.
>
>
> -----------------------------------------------------------------------------------------------------------------
> PHP version
> $ php -v
> PHP 5.2.3 (cli) (built: Jun  8 2007 14:31:21)
> Copyright (c) 1997-2007 The PHP Group
> Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
> -----------------------------------------------------------------------------------------------------------------
> Source code
> <?php
>
> // initialize array
> $cat1 = array();
> $cat1[15] = array('tid' => 15, 'ar_needed' => 'no');
> $cat1[16] = array('tid' => 16, 'ar_needed' => 'no');
> $cat1[17] = array('tid' => 17, 'ar_needed' => 'no');
>
> // print it
> print "\n\$cat1 = ";
> print_r($cat1);
> print "\n";
>
> // iterate using foreach and reference
> foreach ($cat1 as $k => &$v) {
>   if ($k == 15) {
>     $v['ar_needed'] = 'yes'; // dummy change
>   }
> }
>
> // print array
> print "\n\$cat1 = ";
> print_r($cat1);
> print "\n";

At this point, $v is still a reference to $cat1[17]. Try:

unset($v);

> // reiterate
> foreach ($cat1 as $k => $v) {
>   print "\$k = $k\n";
>   print "\$v = ";
>   print_r($v);
>   print "\n";
> }
> ?>
> -----------------------------------------------------------------------------------------------------------------
> OUTPUT
> $cat1 = Array
> (
>     [15] => Array
>         (
>             [tid] => 15
>             [ar_needed] => no
>         )
>     [16] => Array
>         (
>             [tid] => 16
>             [ar_needed] => no
>         )
>     [17] => Array
>         (
>             [tid] => 17
>             [ar_needed] => no
>         )
> )
>
> $cat1 = Array
> (
>     [15] => Array
>         (
>             [tid] => 15
>             [ar_needed] => yes
>         )
>     [16] => Array
>         (
>             [tid] => 16
>             [ar_needed] => no
>         )
>     [17] => Array
>         (
>             [tid] => 17
>             [ar_needed] => no
>         )
> )
>
> $k = 15
> $v = Array
> (
>     [tid] => 15
>     [ar_needed] => yes
> )
>
> $k = 16
> $v = Array
> (
>     [tid] => 16
>     [ar_needed] => no
> )
>
> $k = 17
> $v = Array
> (
>     [tid] => 16
>     [ar_needed] => no
> )
> -----------------------------------------------------------------------------------------------------------------
>
> As seen in the above output all the print statements give expected
> output except the last
> set in the 2nd iteration
> $k = 17
> $v = Array
> (
>     [tid] => 16
>     [ar_needed] => no
> )
>
> the $k looks fine but the $v is not correct for some reason. Any clue
> what could be wrong
> here ?. Also, in the 2nd iteration of the name of the variable is
> changed from $v to $v1
> it displays correctly
> $k = 17
> $v1 = Array
> (
>     [tid] => 17
>     [ar_needed] => no
> )

After the first loop, $v is still a reference to $cat1[17].  Each time
around the last loop, assigning  again to $v will overwrite $cat1[17].

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux