Array Value

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
xionhack
Forum Contributor
Posts: 100
Joined: Mon Nov 10, 2008 9:22 pm

Array Value

Post by xionhack »

Hello, let's say I have an array with the values

Array ( [147] => 1 [148] => [149] => 6 [150] => [151] => )

When trying to update a mysql database, let's say I have two fields 'id' and 'team_id'. What I want is for example, the one with the 'id' = 147, the team_id to be updated to '1', id 148 has nothing set so no update will happen, 149 will be updated to 6, 150 and 151 will have no change. How can I do that? Please let me know if you couldnt understand. Thanks.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Array Value

Post by requinix »

You can use foreach to loop over the array. The key is the id and the value is the team_id.
Of course, only update if the value actually has a, er, value.
User avatar
paqman
Forum Contributor
Posts: 125
Joined: Sun Nov 14, 2004 7:41 pm
Location: Burnaby, BC, Canada

Re: Array Value

Post by paqman »

Code: Select all

 
<?
foreach($array as $id => $team_id)
{
     if(strlen($team_id) > 0)
     {
          //do something with the value
          echo $team_id." - ".$id."<br>";
     }
}
?>
 
Post Reply