Jump to content

array key/value assignment


bcfantasy

Recommended Posts

I'm trying to create an array of team's upcoming gamecounts for a fantasy hockey league.  I want the array, $sched, to have the team abbreviations as keys, and the gamecounts for the upcoming weeks as values in a sub-array of the team abbreviation.  For example, say Anaheim's gamecounts for the next 3 weeks were 4, 4, 3.  I'd want to be able to get Anaheim's week 1 gamecount from $sched['atl']['week1']. I have the schedule saved as a tab delimited text file that I explode into a raw data array.  From there I have trouble.  I'll give you the code and then explain the problem. 
[code]
$filesch = 'nhlschedule.txt';
$fdsch = fopen($filesch, "r") or die("can't open schedule file");
$fileschstring = fread($fdsch, filesize($filesch));
$rawschstats = explode("\n", $fileschstring);

/* give each team their own array */
for ($r = 0; $r < count($rawschstats); $r++)
    {
    $schstats[$r] = explode("\t", $rawschstats[$r]);
    }

/* create new schedule array */
$sched = array();
for ($r = 0; $r < count($schstats); $r++)
    {
    $team = $schstats[$r][0];
    $sched[$team] = array();
    $sched[$team] = $schstats[$r][0];
    $sched[$team]['week1'] = $schstats[$r][1];
    $sched[$team]['week2'] = $schstats[$r][2];
    $sched[$team]['week3'] = $schstats[$r][3];
    }
[/code]

If I echo the key/value immediately after assignment, it works fine. However, if I var_dump($sched) after the second for loop, it shows the keys as the team abbreviations, but the values are also the team abbreviations with the first letter replaced by the 'week3' value. My questions are: 1) What is going wrong here? 2) Is there a better way to get to the result I am looking for, even if there is a coding error here?

I am a PHP newbie, so please correct me on any terminology errors, or anything else for that matter. If for some reason you need the text file you can get it at [url=http://www.bcfantasy.com/testing/nhlschedule.txt]http://www.bcfantasy.com/testing/nhlschedule.txt[/url]. Thanks.
Link to comment
Share on other sites

I [b]THINK[/b] I understand what you're trying to do.

I'm not sure if this has anything to do with your problem, but the line marked by my comment below will CHANGE the value of the array element in $sched[$team] to the current value of $schstats[$r][0]--even thought the line above it created an array as that element.

[code]
/* create new schedule array */
$sched = array();
for ($r = 0; $r < count($schstats); $r++)
    {
    $team = $schstats[$r][0];
    $sched[$team] = array();
    $sched[$team] = $schstats[$r][0];          //<--This line assigns this element to be the same as the value of $schstats[$r][0]
    $sched[$team]['week1'] = $schstats[$r][1];
    $sched[$team]['week2'] = $schstats[$r][2];
    $sched[$team]['week3'] = $schstats[$r][3];
    }
[/code]
If you're wanting to store the team name as a value in the array, change the commented line to:
[code]
$sched[$team]['name'] = $schstats[$r][0];
[/code]
Link to comment
Share on other sites

Thanks doni. I removed that line of code and got what I was looking for.  Essentially:
[code]
$sched => ANA => week1 => 4
                week2 => 4
                week3 => 3
          ATL => week1 => 2
                week2 => 2
                week3 => 3.....etc....
[/code]

Can anyone explain why it was doing this before:
[code]
$sched => ANA => 3NA
          ATL => 3TL
          BOS => 3OS
          BUF => 2UF......etc....
[/code]
Now that my problem is solved, I don't actually [u]need[/u] to know, but it would give me a better understanding of what was going on.  Who knows, maybe some day I'll actually want to replace the first character. 
Link to comment
Share on other sites

I think I can offer an explanation..

First, you set $sched[$team] to a scalar value (a string).  Then you try to access it as an array.

PHP handles that by converting your array index (such as 'week1') into an integer, and treats it as a string offset index.  So:

[code]$sched[$team]['week1'] = 3;
# is interpreted as..
$sched[$team]{0} = 3;[/code]

Hence, the first character is changed to the week3 value, since that's the last assignment you make to the first character.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.