[Phpwm] updating an array

alan dunn alan at dunns.co.uk
Fri Oct 20 12:16:08 BST 2006


Rob, thank you for the suggestion of using in_array. After a frustrating 
couple of hours I found that in_array looks for a match on the array 
element value, not the array key value. So then I tried to set up a two 
dimensional array and tried to run in_array on that. I don't think 
in_array runs against multi-dimension arrays (am I wrong?).

here is my prodarray after the first iteration:

Array ( [8x6_blue] => 2 [7x5_blue] => 4 [3x2_and_pass] => 4 [jobcard] => 
2 )

then my next line has quantity 3 of 8x6_blue

now I want to say if 8x6_blue is in_array prodarray (as a key value) add 
3 to 2

but in_array is saying does 8x6_blue equal '2' or '4' or '4' or '2'
which of course it doesn't

I have tried other configurations but still without success

cheers, alan

Rob Allen wrote:
> alan dunn wrote:
>   
>> 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
>>     
>
> [snip]
>
>   
>>            // 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
>>
>>     
>
> $match will always be 'f' at the if($match == 'f') statement, unless you
> happen to match key1 to key2 on the very last iteration through prodarray.
>
>
> You should move the $match='f' statement to outside the
> foreach($prodarray) loop. Also, set $match to false/true rather than the
> characters t and f.
>
>
> A much better way to handle that problem is to use in_array() rather
> than the inner foreach loop. Something like:
>
>     // loop through second array
>     foreach ($secondarray as $key => $qty)
>     {
>         if(in_array($key, $prodarray))
>         {
>             $prodarray[$key] += $qty;
>         }
>         else
>         {
>             $prodarray[$key] = $qty;
>         }
>     }
>
> Then you don't need to worry about $match at all and you save a PHP
> loop, so the code will be quicker.
>
> 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