Anyone one around who is good with Arrays???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jollyguy
Forum Newbie
Posts: 2
Joined: Sat Oct 01, 2005 1:44 am

Anyone one around who is good with Arrays???

Post 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]
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

your code seems okay. how do you confirm that it creates a new array and doesn't update old one?
jollyguy
Forum Newbie
Posts: 2
Joined: Sat Oct 01, 2005 1:44 am

Post 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.
Post Reply