[Phpwm] updating an array
alan dunn
alan at dunns.co.uk
Thu Oct 19 17:15:15 BST 2006
in response to Rob here is the entire loop:
there are a maximum of four products with varying quantities e1,q1, e2,
q2, e3, q3, e4, q4
$prodarray = array();
// first iteration put first row values into array
if($i == 1){
$prodarray[$e1] = $q1;
if(!empty($e2)){$prodarray[$e2] = $q2;}
if(!empty($e3)){$prodarray[$e3] = $q3;}
if(!empty($e4)){$prodarray[$e4] = $q4;}
$i++;
// for debugging
print_r($prodarray);?><br><?php
} // end if $i = 1
// Now go into second and subsequent iterations through sql output
// put the values into a second array
// and compare every value in second array with first array
// if they match add quantity
// if they don't add a new line to prodarray
else{
// build the array
$secondarray = array();
$secondarray [$e1] = $q1;
if(!empty($e2)){$secondarray [$e2] = $q2;}
if(!empty($e3)){$secondarray [$e3] = $q3;}
if(!empty($e4)){$secondarray [$e4] = $q4;}
print_r($secondarray);?><br><?php
// loop through second array
foreach ($secondarray as $key2 => $qty2){
// comparing values in first array
foreach ($prodarray as $key1 => $qty1){
$match = 'f';
if($key1 == $key2){
//echo"key1=$key1 qty1= $qty1 key2=$key2 qty2 = $qty2
tot = $tot<br>
//prodarraykey1 = $prodarray[$key1]<br> ";
$prodarray[$key1] = &$tot; // here we have tried to
use the & notation
$tot = $tot + $qty2;
$match = 't';}
// if there is no match add the product to prodarray
} // end foreach prodarray
// if there was no match add a new line to prodarray -- this
bit works!
if($match == 'f'){$prodarray[$key2] = $qty2; }
} // end for each second array
} // end else
Rob Allen wrote:
> 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?
>>
>> thanks, alan dunn
>>
>>
>
> Can you post the entire loop?
>
> Regards,
>
> Rob...
>
> _______________________________________________
> Phpwm mailing list
> Phpwm at mailman.lug.org.uk
> https://mailman.lug.org.uk/mailman/listinfo/phpwm
>
>
>
More information about the Phpwm
mailing list