rockinaway Posted April 2, 2007 Share Posted April 2, 2007 How do I use current for an array in an array? Like: $bowler_array = array( 'rockinaway' => array( 'overs' => '0', 'runs' => '0', 'wickets' => '0', ), 'test1' => array( 'overs' => '0', 'runs' => '0', 'wickets' => '0', ), 'test2' => array( 'overs' => '0', 'runs' => '0', 'wickets' => '0', )); Quote Link to comment Share on other sites More sharing options...
rockinaway Posted April 2, 2007 Author Share Posted April 2, 2007 Also I get the Illegal Offset Type error, as I am using an array as a key, anyway to use an array as the key? Or some other way around it? Quote Link to comment Share on other sites More sharing options...
trq Posted April 2, 2007 Share Posted April 2, 2007 Post your code and a description of what it is your trying to do. Quote Link to comment Share on other sites More sharing options...
rockinaway Posted April 2, 2007 Author Share Posted April 2, 2007 $bowler_array = array( 'rockinaway' => array( 'overs' => '0', 'runs' => '0', 'wickets' => '0', ), 'test1' => array( 'overs' => '0', 'runs' => '0', 'wickets' => '0', ), 'test2' => array( 'overs' => '0', 'runs' => '0', 'wickets' => '0', )); $overs = 0; if ($overs < 50) { foreach($bowler_array as $key) { for ($i = 0; $i < 6; $i++) { $runs = rand(-1,6); if ($runs == -1) { $bowler_array[$key]['wickets'] = current($bowler_array[$key]['wickets']) + 1; } else{ $bowler_array[$key]['runs'] = current($bowler_array[$key]['runs']) + $runs; } if ($i == 6) { $bowler_array[$key]['overs'] = current($bowler_array[$key]['overs']) + 1; } else $bowler_array[$key]['overs'] = current($bowler_array[$key]['overs']) + 0.1; } } print_r($bowler_array); } That is my code, very rough at the moment. I have an array of the bowlers, then each bowler has another array of their stats. I am having the overs done by a for and then adding it on, I need to use the current bowler from the array then update the array values inside the bowler array for the separate bowlers.. make sense? Quote Link to comment Share on other sites More sharing options...
rtconner Posted April 2, 2007 Share Posted April 2, 2007 make sense? Not really, I've read this 10 times and cannot figure out what you want or want to do. FYI - current($bowler_array[$key]['runs'] and current($bowler_array[$key]['wickets'] are both illegal code because the values in that data is not an array. current() needs an array, not a number passed into it. current($bowler_array[$key]) would be valid code. Quote Link to comment Share on other sites More sharing options...
rockinaway Posted April 2, 2007 Author Share Posted April 2, 2007 Oh I get it.... Right I updated it.. $bowler_array = array( 'rockinaway' => array( 'overs' => '0', 'runs' => '0', 'wickets' => '0', ), 'test1' => array( 'overs' => '0', 'runs' => '0', 'wickets' => '0', ), 'test2' => array( 'overs' => '0', 'runs' => '0', 'wickets' => '0', )); $overs = 0; if ($overs < 50) { foreach($bowler_array as $key) { reset($bowler_array); for ($i = 0; $i < 6; $i++) { $runs = rand(-1,6); if ($runs == -1) { $bowler_array[$key]['wickets'] = $bowler_array[$key]['wickets'] + 1; } else{ $bowler_array[$key]['runs'] = $bowler_array[$key]['runs'] + $runs; } if ($i == 6) { $bowler_array[$key]['overs'] = $bowler_array[$key]['overs'] + 1; } else $bowler_array[$key]['overs'] = $bowler_array[$key]['overs'] + 0.1; } next($bowler_array); } print_r($bowler_array); } But there still is the problem with using the key as I need to refer to the name of the bowlers generally, but I can't use them like $value in a foreach because it gives illegal offset, so what should I do.. Quote Link to comment Share on other sites More sharing options...
dough boy Posted April 2, 2007 Share Posted April 2, 2007 1. I don't think you need to call next() as in a foreach it is understood. 2. Why are you looping over each bowler 6 times? 3. You are saying if ($i == 6), but your for loop will stop at 5 and never reach 6. Quote Link to comment Share on other sites More sharing options...
rockinaway Posted April 2, 2007 Author Share Posted April 2, 2007 $bowler_array = array( 'rockinaway' => array( 'overs' => '0', 'runs' => '0', 'wickets' => '0', ), 'test1' => array( 'overs' => '0', 'runs' => '0', 'wickets' => '0', ), 'test2' => array( 'overs' => '0', 'runs' => '0', 'wickets' => '0', )); $overs = 0; if ($overs < 50) { foreach($bowler_array as $key => $value) { for ($i = 0; $i < 7; $i++) { $runs = rand(-1,6); if ($runs == -1) { $value['wickets'] = $value['wickets'] + 1; } else{ $value['runs'] = $value['runs'] + $runs; } if ($i == 6) { $value['overs'] = $value['overs'] + 1; } else $value['overs'] = $value['overs'] + 0.1; } } print_r($bowler_array); } Got it without any errors, but it doesn't work. Sorted out the other stuff you mentioned, but it doesn't work, doesn't update the array values :S Quote Link to comment Share on other sites More sharing options...
Barand Posted April 2, 2007 Share Posted April 2, 2007 Would it hurt too much if you told what you are trying to do? "It doesn't work" tells us nothing. Just posting code that "doesn't work" only shows us what you're NOT trying to do. Quote Link to comment Share on other sites More sharing options...
rockinaway Posted April 2, 2007 Author Share Posted April 2, 2007 OK.. I try explaining as best as I can. Right there is an array of bowlers who each have another array containing their stats. Then I use a foreach to update the stats of the bowlers. for ($i = 0; $i < 7; $i++) { $runs = rand(-1,6); if ($runs == -1) { $value['wickets'] = $value['wickets'] + 1; } else{ $value['runs'] = $value['runs'] + $runs; } if ($i == 6) { $value['overs'] = $value['overs'] + 1; } else $value['overs'] = $value['overs'] + 0.1; } This controls the overs, so 6 balls. The $runs selects a random number and if the nuber in a minus then it is a wicket, else the runs are added to the run total of the bowler. Then if $i is == to 6 then it adds 1 over to the bowler, else it adds 0.1. That is it really. Quote Link to comment Share on other sites More sharing options...
rockinaway Posted April 2, 2007 Author Share Posted April 2, 2007 Anyone? Quote Link to comment Share on other sites More sharing options...
rtconner Posted April 2, 2007 Share Posted April 2, 2007 Oh my. So this is cricket. I guess my full lack of knowledge about anything regarding cricket doe not help. Plus you assuming I know something about cricket does not help either. I'll just assume you know why you want random numbers, because I can't even begin to guess why. Maybe try this out. I see why you might have had problems solving this without a good understanding of foreach. No promises on syntax, sorry if I missed a semi colon while (list($key, $bowler) = each($bowler_array)) { // loop through each bowler for ($i = 0; $i < 6; $i++) { // i have no idea why we are looping 6 times inside of each bowler $runs = rand(-1,6); if ($runs == -1) $bowler_array[$key]['wickets']++; // add one to the wicket else $bowler_array[$key]['runs'] += $runs; // runs added to run total? if ($i == 6) $bowler_array[$key]['overs'] += 1; // add one to overs else $bowler_array[$key]['overs'] += 0.1; // add .1 to overs } } Quote Link to comment Share on other sites More sharing options...
Barand Posted April 2, 2007 Share Posted April 2, 2007 One observation after 5 balls the overs value is 0.5. On the 6th ball you add 1, bringing the overs value to 1.5 when it should be 1. So shouldn't you add 0.5 on the 6th ball? Quote Link to comment Share on other sites More sharing options...
Barand Posted April 2, 2007 Share Posted April 2, 2007 As already pointed out, if you have for ($i = 0; $i < 6; $i++), the $i never has the value 6 within the loop. try if ($overs < 50) // not sure where $overs is incremented elsewhere in your code { foreach($bowler_array as $bowler => $stats) { for ($i=1; $i<=6; $i++ ) { $runs = rand(-1, 6); if ($runs==-1) { $stats['wickets']++; } else { $stats['runs'] += $runs; } } $stats['overs']++; $bowler_array[$bowler] = $stats; if (++$overs==50) break; // exit the loop after 50th over } } echo '<pre>', print_r($bowler_array, true), '</pre>'; Quote Link to comment Share on other sites More sharing options...
rtconner Posted April 2, 2007 Share Posted April 2, 2007 Hey Barand, $bowler_array[$bowler] = $stats; Thats why I did the list, each in my example. Both work though I guess. Quote Link to comment Share on other sites More sharing options...
Barand Posted April 2, 2007 Share Posted April 2, 2007 Personal preference I guess, I prefer foreach() to list()=each(). (off topic) In answer to your question "i have no idea why we are looping 6 times inside of each bowler", a bowler bowls an "over" of 6 balls. From each ball the batsman can score from 0 to 6 runs by running between the wickets that many times (the wickets are 22 yards, or 66ft or 1 chain apart), or he could be "out" (caught, bowled, leg before wicket, stumped or runout), hence the -1 option, the bowler gets a "wicket" and the next batsman comes out to the field and is in. After the bowler has bowled his over, they change ends and the bowler from the other end of the wicket bowls an over at the other batsman (although if an odd of runs was scored off the last ball, it could at the same batsman). It does seem strange, therefore, that we have here an array of three bowlers, when bowlers, like batsmen, are in pairs, one at each end of the wicket. This goes on for five days a week over a five to ten week period, hence the obvious appeal. I'm sure that clears it all up for you. Quote Link to comment Share on other sites More sharing options...
rockinaway Posted April 3, 2007 Author Share Posted April 3, 2007 As already pointed out, if you have for ($i = 0; $i < 6; $i++), the $i never has the value 6 within the loop. try if ($overs < 50) // not sure where $overs is incremented elsewhere in your code { foreach($bowler_array as $bowler => $stats) { for ($i=1; $i<=6; $i++ ) { $runs = rand(-1, 6); if ($runs==-1) { $stats['wickets']++; } else { $stats['runs'] += $runs; } } $stats['overs']++; $bowler_array[$bowler] = $stats; if (++$overs==50) break; // exit the loop after 50th over } } echo '<pre>', print_r($bowler_array, true), '</pre>'; That is suitable I think, may need to tweak it for my needs slightly. WIll try it when I get home. Quote Link to comment Share on other sites More sharing options...
rockinaway Posted April 3, 2007 Author Share Posted April 3, 2007 It Works! Will tweak it abit for the full working I want, and the 50 overs thing Thanks alot.. Barand - you must be a cricket fan, wanna join my cricket site? 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.