Page 1 of 1

Anyone one around who is good with Arrays???

Posted: Sat Oct 01, 2005 1:53 am
by jollyguy
feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi, 

I have a query which generates an array from the database results returened:

Code: Select all

$getrel = mssql_query('select * from products where vendor_code=151'); 
while($gotrel = mssql_fetch_array($getrel)) 
{ 
$relation[$gotrel[id]] = array ('id' => $gotrel[id], 
'rvccode' => $gotrel[vendor_ccode], 
'rvsname' => $gotrel[vendor_sname], 
'rpcat' => $gotrel[prod_cat], 
'rpnm' => $gotrel[prod_nm], 
'rused' => '0'); 
}
Now I go through this array in steps and first want to identify all products with prod_cat="Hardware" and set their 'rused' element to '1'

Code: Select all

$fld1 = 'rpcat'; 
while (list ($key, $val) = each ($relation)){ 
if ($relation[$key][$fld1] == 'Hardware') { $relation[$key]['rused'] = '1'; 
} 
}
The code does go into the loop but does not update the element in original array $relation but instead creates a new array instead. Can anyone let me know how to update the array element in the original array.

Thanks


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Sat Oct 01, 2005 3:52 am
by n00b Saibot
your code seems okay. how do you confirm that it creates a new array and doesn't update old one?

Posted: Sat Oct 01, 2005 4:07 am
by jollyguy
Hi,

Thanks for the prompt reply.

Actually the problem was that i wasnt passing the array by reference. Once i started doing it by using '&' infront of the array name in the function declaration it worked fine.