Forum Moderators: coopster

Message Too Old, No Replies

Get references in foreach loop

         

Bernard Marx

7:32 pm on Sep 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a little code:

$arr = array
(
'key0' => array(0,1),
'key1' => array(2,3),
'key2' => array(4,5)
);


foreach( $arr as $key => $sub )
{
$sub[1] = 'X';
}

Being a Javascripter, I expected that this would mean that:

echo $arr['key0'][1]; [blue]// 'X'[/blue]

It took me a while to figure out that the sub-arrays were being copied into the variable, $sub, so anything I do inside the loop has no effect on my data structure.

The Question

Is there some syntax that I can use to force the

foreach
enumeration to throw out references rather than values?

coopster

7:49 pm on Sep 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You are correct in that PHP array assignment always involves value copying. You need to use the reference operator to copy an array by reference.
foreach( $arr as $key => &$sub )

Resources with documentation concerning references:
Arrays [php.net]
foreach [php.net]

Bernard Marx

8:07 pm on Sep 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks coopster. I had tried that one acting on a hunch, and it didn't work.
A quick peek at the PHP manual:

As of PHP 5, you can easily modify array's elements by preceding $value with &. This will assign reference instead of copying the value.

..and I'm running PHP 4.

Would it be a headache trying to install PHP 5 alongside it?

I imagine that would only involve swapping a couple of Apache config directives.
I also like running PHP in WSH - that's the unknown factor it this point.

coopster

8:23 pm on Sep 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I almost edited that and stated that it was PHP5 syntax. In PHP4 I just use the full array definition to update the original.
foreach( $arr as $key => $sub ) {  
$arr[$key][1] = 'X';
}

Bernard Marx

8:56 pm on Sep 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I did something similar. I made the outer array numerical, and included the key strings as the first member of the subs. Then I scrapped the whole thing and stopped trying to be so clever.

I'm doing this for a college assignment - server-side forms whatnot. You can imagine. I started off by trying to apply the kind of approach that I would on the client-side, with all the configureables nicely mapped out at the top. I just got into a mess.

If there were a book entitled, Learning PHP for users of languages beginning with J, it would begin thusly:
"At least one of the P's in PHP stands for 'Perverse'".
-----
If you are familiar with the works of the band, Gong, you will know that PHP stands for something else entirely.

coopster

9:41 pm on Sep 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I didn't even realize that the PHP Manual page has an updated example under Collections in that link I dropped earlier.

Changing values of the array directly is possible since PHP 5 by passing them as reference. 
Prior versions need workaround:
Example 11-8. Collection
<?php
// PHP 5
foreach ($colors as &$color) {
$color = strtoupper($color);
}
unset($color); /* ensure that following writes to
$color will not modify the last array element */
// Workaround for older versions
foreach ($colors as $key => $color) {
$colors[$key] = strtoupper($color);
}
print_r($colors);
?>

I have read that page many times in the past, but haven't reviewed it for quite some time so I didn't even realize that example existed. I have to remember that the HTML manual pages are continually being updated as well! It's not like languages and manuals of the past where you get an update sheet with revision codes in the sidebar.

Anyway, if you get stuck on PHP nuances, post 'em here. You've offered so much great assistance over in the js forum that we enjoy returning the favor.

Bernard Marx

10:00 pm on Sep 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Will do. I'll try not to dig more deeply into PHP than necessary in the coming weeks, as I have mySQL to fit in too, but after that..

I'll play with that snippet when I get to my PHP 5 installation at college.

I'm a big fan & collector of CHM files, so I view the PHP manual like that. I'd better watch out for updates too.