Array Help

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
Crew
Forum Newbie
Posts: 16
Joined: Fri Jul 15, 2005 4:05 am

Array Help

Post by Crew »

Hi

I'm currently using the following to put data into an array.

Code: Select all

$football[$team_id++] = array("win" => $wins,"loss" => $losses);
What I cant figure out is how I update the wins or losses if the array already holds a previous record for the same $team_id. Say $wins = $wins + 1 kinda thing? Arrays are my weakest area and they're so important =/, can anyone shed some light?

Andy
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: Array Help

Post by feyd »

Code: Select all

// increment wins
$football[$team_id]['wins']++;

// increment losses
$football[$team_id]['loss']++;
Crew
Forum Newbie
Posts: 16
Joined: Fri Jul 15, 2005 4:05 am

Post by Crew »

How would you go about sorting a large array thats as follows

Code: Select all

$football = array();
$football[$i] = array('team_id' => 0,'pld' => 0,'wins' => 0,'draws' => 0,'losses' => 0,'for' => 0,'against' => 0,'gd' => 0,'pts' => 0);
All of those values have been filled with league table results but how would i go about sorted in starting with points then goal difference?

I've found the following code in a tutorial but am unsure how to edit it to my needs

Code: Select all

function compare($x, $y)
{
 if ( $x[6] == $y[6] )
  return 0;
 else if ( $x[6] < $y[6] )
  return -1;
 else
  return 1;
}

usort($football, 'compare');
Thanks Andy
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

function compare( $a, $b ) {
  if($a['pts'] == $b['pts']) {
    if($a['gd'] == $b['gd']) {
      return 0;
    }
    return ($a['gd'] > $b['gd'] ? 1 : -1);
  }
  return ($a['pts'] > $b['pts'] ? 1 : -1);
}

usort($array,'compare');
Crew
Forum Newbie
Posts: 16
Joined: Fri Jul 15, 2005 4:05 am

Post by Crew »

I tell you what your a star I had just finished my way making one looking at the php manual but this is better :) ta
Post Reply