NYCPHP Meetup

NYPHP.org

[nycphp-talk] Multidimensional Arrays?

putamare jeffknight at mac.com
Wed Jul 7 15:01:44 EDT 2004


Well Harvey, assuming you defined something like

$all_teams['Team 1'][2002] = 8;
$all_teams['Team 1'][2003] = 9;
$all_teams['Team 1'][2004] = 22;

$all_teams['Team 2'][2002] = 18;
$all_teams['Team 2'][2003] = 94;
$all_teams['Team 2'][2004] = 12;

$all_teams['Team 3'][2002] = 23;
$all_teams['Team 3'][2003] = 44;
$all_teams['Team 3'][2004] = 45;

The code

foreach ($all_teams as $team)
	foreach ($team as $year => $score)
		$all_teams['sum'][$year] += $score ;

would give you a brute force calculation of what you're looking for, 
use a

print_r($all_teams)

to get the hang of it. Note also that

array_sum($all_teams['Team 1']);

would give you the sum of 'Team 1' across all three years. Experiment  
with

$all_teams[2002]['Team 1'] = 8;
$all_teams[2002]['Team 2'] = 18;
$all_teams[2002]['Team 3'] = 23;

$all_teams[2003]['Team 1'] = 9;
$all_teams[2003]['Team 2'] = 94;
$all_teams[2003]['Team 3'] = 44;

$all_teams[2004]['Team 1'] = 22;
$all_teams[2004]['Team 2'] = 12;
$all_teams[2004]['Team 3'] = 45;

run a few more print_r's and you'll be well on your way to getting the 
hang of multidimensional arrays. And don't forget

http://www.php.net/manual/en/language.types.array.php
and
http://www.php.net/manual/en/ref.array.php


On Jul 7, 2004, at 1:14 PM, harvey wrote:

> Hello,
>
> I have code that loops through the database and outputs something like 
> the
> following:
>
>
> TEAM 1
>
> Year 1    Year 2     Year 3
>
> 8         9          22
>
> TEAM 2
>
> Year 1     Year 2     Year 3
>
> 18         94         12
>
> TEAM 3
>
> Year 1     Year 2     Year 3
>
> 23         44         45
>
> ALL TEAMS
>
> Year 1     Year 2     Year 3
>
> ??         ??         ??
>
>
> I can't figure out how to easily get the ?? totals. I think I want a 
> major
> array of three minor arrays (I probably just invented my own 
> vocabulary):
> the major array would increment by one each year; the three minor 
> arrays
> would hold the Year 1 values, Year 2 values, and Year 3 values.
> Conceptually, it would be something like $all_teams[$year_id][ ]. Is
> something like that possible? I know that php allows multidimensional
> arrays, but I guess I don't know the proper syntax or usage.
>
> Thanks in advance for any help!
>
> Harvey
>
>
> _______________________________________________
> talk mailing list
> talk at lists.nyphp.org
> http://lists.nyphp.org/mailman/listinfo/talk




More information about the talk mailing list