[Phpwm] updating an array

David Goodwin david at codepoets.co.uk
Thu Oct 19 15:06:42 BST 2006


alan dunn wrote :
> I am simply(!) trying to read through an array comparing the key with a 
> string (product code) and where I find a match add the corresponding 
> quantity to the existing quantity.
> 
> if($match) {$prodarray[$key1] = $prodarray[$key1] + $qty2; }
> 
> If I echo $prodarray[$key1] immediately after this line I get the 
> increased value. But after continuing to cycle through the array the 
> changed value is lost.
> 
> I have some recollection of a group discussion about arrays being read 
> into memory, or proxy values or some such? Can anyone shed light on how 
> I get that value into the array?

Right, to elabourate on my previous post; I suspect you're doing your
matching/searching within a function.

I can do the following :

$list = array(5,4,3,2,1);
foreach($list as $key => $value) {
    if($value > 3) {
        $list[$key] = 9;
    }
}

print_r($list);

which will show : 9,9,3,2,1


If I change it to :

<?php
$list = array(5,4,3,2,1);
function whatever($list) {
    foreach($list as $key => $value) {
        if($value > 3) {
            $list[$key] = 9;
        }
    }
}
whatever($list);
print_r($list);

I'll get : 5,4,3,2,1

This is because of the fact that PHP4 (and PHP5 when not using objects)
does Pass by Value, not Pass by Reference.
(Java always does Pass by Reference).

i.e. When it runs inside the function it is a copy of the original
variable passed in; so the whatever function's $list is a copy of the
global $list. 

If we want to fix this, we can either :

a) Return $list from the function and assign it to $list globally
(globally = outside a function scope)
or
b) Pass by reference


To pass by reference you should put an '&' sign infront of variables in
the function declaration i.e.
$list = array(5,4,3,2,1);
function whatever(&$list) {
    foreach($list as $key => $value) {
        if($value > 3) {
            $list[$key] = 9;
        }
    }
}
whatever($list);
print_r($list);

will give 9,9,3,2,1

Or

$list = array(5,4,3,2,1);
function whatever(&$list) {
    foreach($list as $key => $value) {
        if($value > 3) {
            $list[$key] = 9;
        }
    }
    return $list;
}
$list = whatever($list);
print_r($list);

which will also give 9,9,3,2,1


>From a maintenance point of view, if you're going to use pass by
reference, make sure you use the '&' signs in the function signature,
and not at runtime when callign the function

i.e.
Good: function whatever(&$list) { ...... }
Bad : $list = whatever(&$list);

The reasoning being that you (the programmer) have to remember to put
the '&' in in the latter case, but not in the former, which could lead
to hard to track down bugs.


I'll assume that helps, and answers your question/problem, even if the
context isn't quite right.

David
</good-deed-of-the-day :-) >
-- 
David Goodwin 

[ david at codepoets dot co dot uk ]
[ http://www.codepoets.co.uk       ]



More information about the Phpwm mailing list