rockinaway Posted February 18, 2010 Share Posted February 18, 2010 I have an array which has several values: $batsmen[] = array ( 'id' => $array['id'], 'name' => $array['name'], 'ability' => $array['ability'], 'strength' => $array['strength'], 'out' => 0, 'in' => 0, 'runs' => 0 ); I then have a foreach loop, to loop through this array. In the foreach loop I have the following code: foreach ($batsmen as $bat_key => $batsman) { $batsman['in'] = 1; } So the 'in' should be set to one. However, this isn't being done. I am not sure why, but it always remains at zero. What could the issue be? Link to comment https://forums.phpfreaks.com/topic/192512-issue-with-array/ Share on other sites More sharing options...
flappy_warbucks Posted February 18, 2010 Share Posted February 18, 2010 it's because you have a spelling mistake in the code. foreach ($batsmen as $bat_key => $batsman) { $batsman['in'] = 1; } unless you are creating a new batsman array, the code should be: foreach ($batsmen as $bat_key => $batsman) { $batsmen['in'] = 1; } Link to comment https://forums.phpfreaks.com/topic/192512-issue-with-array/#findComment-1014305 Share on other sites More sharing options...
rockinaway Posted February 18, 2010 Author Share Posted February 18, 2010 That makes no difference. I have been changing values, for example $batsman['runs'] in that foreach, and it works perfectly fine. Even changing the spelling doesn't help. I should mention that there is another foreach and a while inside the main foreach statement that I have given. However, as I mentioned, I am able to edit other values in the array like the 'runs'. So why can't I edit this? Link to comment https://forums.phpfreaks.com/topic/192512-issue-with-array/#findComment-1014309 Share on other sites More sharing options...
rockinaway Posted February 18, 2010 Author Share Posted February 18, 2010 Actually, my apologies. The value is setting, but when I retrieve it with another foreach, I am not getting the correct values for any variable in the array. I will give the code soon once I tidy it up. Thanks Link to comment https://forums.phpfreaks.com/topic/192512-issue-with-array/#findComment-1014312 Share on other sites More sharing options...
rockinaway Posted February 18, 2010 Author Share Posted February 18, 2010 Okay. How can I search an array for a match and return ONLY the first value that matches? Link to comment https://forums.phpfreaks.com/topic/192512-issue-with-array/#findComment-1014316 Share on other sites More sharing options...
rockinaway Posted February 18, 2010 Author Share Posted February 18, 2010 Okay, here is all the code: <?php // Error reporting error_reporting(E_ALL); // Connect DB mysql_connect("xxx", "xxxx", "xxxx") or die(mysql_error()); // Select DB mysql_select_db("xxx") or die(mysql_error()); // MySQL queries $array_batsmen = mysql_query('SELECT id, name, ability, strength FROM wtw_players WHERE team_id = 1 ORDER BY id ASC'); $array_bowlers = mysql_query('SELECT id, name, ability, strength FROM wtw_players WHERE team_id = 2'); // Start table $data = '<table width="600px">'; // Create blank scorecard while ($array_bat = mysql_fetch_array($array_batsmen)) { $data .= '<tr><td>'.$array_bat['name'].'</td><td></td><td></td><td>0</td></tr>'; } // Set up arrays for players $array_bow = $array_bats = array(); // Reset query mysql_data_seek($array_batsmen,0); $in_one = $in_two = $it = ''; // Set up arrays of batsmen while ($array = mysql_fetch_array($array_batsmen)) { if ($it < 3 && $in_one == '') { $in_one = $array['id']; $it++; } else if ($it < 3 && $in_two == '') { $in_two = $array['id']; $it++; } $batsmen[] = array ( 'id' => $array['id'], 'name' => $array['name'], 'ability' => $array['ability'], 'strength' => $array['strength'], 'out' => 0, 'in' => 0, 'runs' => 0 ); } // Bowler Array while ($array = mysql_fetch_array($array_bowlers)) { $bowlers[] = array ( 'name' => $array['name'], 'ability' => $array['ability'], 'strength' => $array['strength'] ); } // Reset both queries mysql_data_seek($array_bowlers,0); mysql_data_seek($array_batsmen,0); $runs = $outs = $balls = $overs = 0; $played = array(); // Loop foreach ($batsmen as $bat_key => $batsman) { $batsman['in'] = 1; while ($batsman['out'] == 0 && $balls <= 295 && $outs < 10) { // Loop through bowlers foreach ($bowlers as $bow_key => $bowler) { // Set the initial number $number = rand(0, 190); // Ability of batsman if ($batsman['ability'] > 90) $number += 30; else if ($batsman['ability'] > 70) $number += 15; else if ($batsman['ability'] > 50) $number += 2; else $number = $number; // Strength of batsman if ($batsman['strength'] > 90) $number += 15; else if ($batsman['strength'] > 70) $number += 10; else if ($batsman['strength'] > 50) $number += 5; else $number = $number; // Depending on overs if ($balls > 270) $number += 30; else if ($balls > 120) $number -= 10; // Ability if ($bowler['ability'] > 90) $number -= 30; else if ($bowler['ability'] > 70) $number -= 15; else if ($bowler['ability'] > 50) $number -= 2; else $number = $number; // If batsman has made a huge total of runs, we need to knock some numbers off - more likely to get out if ($batsman['runs'] > 200) $number -= 70; else if ($batsman['runs'] > 100) $number -= 30; // Finally sort out runs if ($number > 190) $run = 6; else if ($number > 170) $run = 4; else if ($number > 160) $run = 3; else if ($number > 100) $run = 2; else if ($number > 50) $run = 1; else if ($number > 10) $run = 0; else if ($balls > 120 && $number > 0) $run = 0; else $run = -1; // Increase number of balls $balls += 1; // Are they out? if ($run == -1) { $batsman['out'] = 1; $played[] = $batsman['id']; $find = '<tr><td>'.$batsman['name'].'</td><td></td><td></td><td>0</td></tr>'; $replace = '<tr><td>'.$batsman['name'].'</td><td></td><td>'.$bowler['name'].'</td><td>'.$batsman['runs'].'</td></tr>'; $data = str_replace($find, $replace, $data); foreach ($batsmen as $key => $value) { if ($value['out'] == 0 && $value['in'] == 0 && $value['id'] != $in_one && $value['id'] != $in_two) { if (!in_array($value['id'], $played) && $in_one == $batsman['id']) { $in_one = $value['id']; next($batsmen); echo 'cccc'; } else if (!in_array($value['id'], $played) && $in_two == $batsman['id']) { $in_two = $value['id']; next($batsmen); echo 'dddd'; } else echo 'test('.$in_one.','.$batsman['id'].')'; } } } else { $batsman['runs'] += $run; $runs += $run; if ($run == 1 || $run == 3) { $test = $batsman['id']; $test2 = $in_two; if ($in_one == $batsman['id']) { $in_two == $batsman['id']; echo 'change one'; } else if ($in_two == $batsman['id']) { $in_one == $batsman['id']; echo 'change two'; } } } } // Count outs if ($batsman['out'] == 1) $outs += 1; } } $rem = $balls % 6; $balls_full = $balls - $rem; $overs = $balls_full/6; $data .= '<tr><td>'.$outs.'</td><td>'.$overs.'.'.$rem.'</td><td>'.$runs.'</td></tr>'; $data .= '</table>'; echo $data; ?> There are a couple of echo's that are for testing purposes. Basically, I want to loop through like in a cricket game. Everything seems to work fine, except when a batsman gets out, the values don't change properly - and that is the issue I am having with the value 'in' not setting properly/not returning properly when the array is checked again. Any imporvements to the code are appreciated. Link to comment https://forums.phpfreaks.com/topic/192512-issue-with-array/#findComment-1014339 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.