phpscriptcoder Posted October 26, 2008 Share Posted October 26, 2008 Is there anyway to rewrite this code without using a while loop? foreach ( $BuildArray as $Node => $Item ) { if (!$UnFinished) { $Element = $Item[0]; $Count = $Item[1]; $BuildTime = $Item[2]; while (!$UnFinished ) { if ( $Count > 0 ) { $CurrentPlanet['b_hangar'] -= $BuildTime; $Builded[$Element]++; $CurrentPlanet[$resource[$Element]]++; $Count--; if ($Count == 0) { break; } } else { $UnFinished = true; break; } } } if ( $Count != 0 ) { $CurrentPlanet['b_hangar_id'] .= $Element.",".$Count.";"; } } This is in a game script I'm trying to fix up (the code is very bad). The problem is that $Count is often in the millions, and my host has a max_execution_time of 5 seconds. Thanks! I can give more details about the variables if needed. Quote Link to comment https://forums.phpfreaks.com/topic/130126-how-can-i-rewrite-this-code/ Share on other sites More sharing options...
sasa Posted October 26, 2008 Share Posted October 26, 2008 <?php foreach ( $BuildArray as $Node => $Item ) { if (!$UnFinished) { $Element = $Item[0]; $Count = $Item[1]; $BuildTime = $Item[2]; //while (!$UnFinished ) { if ( $Count > 0 ) { $CurrentPlanet['b_hangar'] -= $BuildTime * $Count; $Builded[$Element] += $Count; $CurrentPlanet[$resource[$Element]] += $Count; $Count = 0; //if ($Count == 0) { // break; //} //} else { $UnFinished = true; //break; } //} } if ( $Count != 0 ) { $CurrentPlanet['b_hangar_id'] .= $Element.",".$Count.";"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/130126-how-can-i-rewrite-this-code/#findComment-674892 Share on other sites More sharing options...
phpscriptcoder Posted October 27, 2008 Author Share Posted October 27, 2008 <?php foreach ( $BuildArray as $Node => $Item ) { if (!$UnFinished) { $Element = $Item[0]; $Count = $Item[1]; $BuildTime = $Item[2]; //while (!$UnFinished ) { if ( $Count > 0 ) { $CurrentPlanet['b_hangar'] -= $BuildTime * $Count; $Builded[$Element] += $Count; $CurrentPlanet[$resource[$Element]] += $Count; $Count = 0; //if ($Count == 0) { // break; //} //} else { $UnFinished = true; //break; } //} } if ( $Count != 0 ) { $CurrentPlanet['b_hangar_id'] .= $Element.",".$Count.";"; } } ?> Thank you for that, works great! Quote Link to comment https://forums.phpfreaks.com/topic/130126-how-can-i-rewrite-this-code/#findComment-676039 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.