Page 1 of 1

Array Help

Posted: Fri Oct 07, 2005 11:58 am
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

Re: Array Help

Posted: Fri Oct 07, 2005 12:03 pm
by feyd

Code: Select all

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

// increment losses
$football[$team_id]['loss']++;

Posted: Fri Oct 07, 2005 2:41 pm
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

Posted: Fri Oct 07, 2005 2:55 pm
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');

Posted: Fri Oct 07, 2005 3:00 pm
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