bcfantasy Posted November 6, 2006 Share Posted November 6, 2006 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. Quote Link to comment Share on other sites More sharing options...
doni49 Posted November 6, 2006 Share Posted November 6, 2006 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] Quote Link to comment Share on other sites More sharing options...
bcfantasy Posted November 6, 2006 Author Share Posted November 6, 2006 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. Quote Link to comment Share on other sites More sharing options...
btherl Posted November 6, 2006 Share Posted November 6, 2006 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. Quote Link to comment Share on other sites More sharing options...
bcfantasy Posted November 6, 2006 Author Share Posted November 6, 2006 Excellent. Thanks btherl. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.