cry of war Posted July 21, 2007 Share Posted July 21, 2007 how would i add the numerical of several different arrayed variables only chooseing the ones i want and set them to equal like $somenumber for example $somenumber = $info[1] + $info[37] + $info[34] + $info[12] + $info[88] all $info[x] are equal to somenumber Quote Link to comment https://forums.phpfreaks.com/topic/61059-php-math/ Share on other sites More sharing options...
cooldude832 Posted July 21, 2007 Share Posted July 21, 2007 you got it right might want to quote it also if they are strings its a bit different, if they are integers that will work $number = ($info[37] + $info[1] +$info[5] +...); if its strings use $number = $info[37].$info[1].$info[5].$...; Quote Link to comment https://forums.phpfreaks.com/topic/61059-php-math/#findComment-303866 Share on other sites More sharing options...
cry of war Posted July 21, 2007 Author Share Posted July 21, 2007 hmmm what should i do if the numbers in the array are from a explode() array and I want to get the sum of all the numbers together Quote Link to comment https://forums.phpfreaks.com/topic/61059-php-math/#findComment-303873 Share on other sites More sharing options...
cooldude832 Posted July 21, 2007 Share Posted July 21, 2007 can I see some code? you mean you want something like? <?phph $nums = "57,45,82,1,56,78,69,55); $info = explode(",",$nums); $sum = array_sum($info); ?> Quote Link to comment https://forums.phpfreaks.com/topic/61059-php-math/#findComment-303877 Share on other sites More sharing options...
cry of war Posted July 21, 2007 Author Share Posted July 21, 2007 $armynouu is what im trying to get and this is what the user is inputting into the text area recon Mission Report Under the cover of night, your 1,000,000 Covert Ops sneak into the realm of overburnit . Your Covert Ops move stealthily through the lands of overburnit undetected. They are able to gather many documents recording the status of weapons, army size and preparedness, and fortifications. Unfortunately, in order to avoid detection, they are not able to provide complete information. Your Chief of Intelligence provides you with the information gathered: Army Size: Unit Type Attackers Defenders Untrained bot 0 0 0 Units 0 0 7227 Human Form Units 100000 930000 1478941 Military Stats Strike Action: 0 Defensive Action ??? Covert Action: 60,353,919,808 AntiCovert Action 0 Covert Skill: 22 Covert Operatives: 1050000 Anti-Covert Skill: 21 Anti-Covert Operatives: 0 Attack Turns: 633 Unit Production: 4544 <?php // if page is not submitted to itself echo the form if (!isset($_POST['submit'])) { ?> <html> <head> <title>Personal INFO</title> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> PLEASE CLICK INSIDE OF THE SGW SPY LOG AREA AND PRESS CTRL+A THEN CTRL+C THEN CLICK IN THE TEXT AREA BELOW WHERE IT SAYS "INSERT HERE". <textarea rows="20" cols="20" name="info" wrap="physical">INSERT HERE</textarea><br /> ENTER ID: <INPUT TYPE="TEXT" name="id" value="INPUT ID HERE" /><br /> <input type="submit" value="submit" name="submit" /> </form> <?PHP } else { $info = $_POST['info']; $id = $_POST['id']; $find = array('bot', 'Units', 'Human Form troop2a', 'Mercenaries', 'Jaffa Army', 'Super Soldiers', 'Mercenaries', 'Soldier', 'NID Agent', 'Mercenaries', 'Drones', 'Clones', ' ', ','); $replace = array('troop1a', 'troop2a', 'troop3a', 'troop1b', 'troop2b', 'troop3b', 'troop1c', 'troop2c', 'troop3c', 'troop1d', 'troop2d', 'troop3d', ' ', ''); $info = str_replace($find, $replace, $info); $info2 = explode(" ", $info); $armynouu = ($info[79] + $info[80] + $info[81] + $info[83] + $info[84] + $info[87] + $info[88] + $info[89] + $info[109] + $info[115]); echo "<pre><a href=http://www.stargatewars.com/stats.php?id=$id target=_blank>$race $info2[16] $race ($armynouu ~ $info2[121]</a><br /></pre>"; for($i = 0; $i <= 200; $i++) { echo "Piece $i = $info2[$i] <br />"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/61059-php-math/#findComment-303879 Share on other sites More sharing options...
sasa Posted July 21, 2007 Share Posted July 21, 2007 try <?php $info = 'recon Mission Report Under the cover of night, your 1,000,000 Covert Ops sneak into the realm of overburnit . Your Covert Ops move stealthily through the lands of overburnit undetected. They are able to gather many documents recording the status of weapons, army size and preparedness, and fortifications. Unfortunately, in order to avoid detection, they are not able to provide complete information. Your Chief of Intelligence provides you with the information gathered: Army Size: Unit Type Attackers Defenders Untrained bot 0 0 0 Units 0 0 7227 Human Form Units 100000 930000 1478941 Military Stats Strike Action: 0 Defensive Action Huh Covert Action: 60,353,919,808 AntiCovert Action 0 Covert Skill: 22 Covert Operatives: 1050000 Anti-Covert Skill: 21 Anti-Covert Operatives: 0 Attack Turns: 633 Unit Production: 4544'; $find = array('bot', 'Units', 'Human Form troop2a', 'Mercenaries', 'Jaffa Army', 'Super Soldiers', 'Mercenaries', 'Soldier', 'NID Agent', 'Mercenaries', 'Drones', 'Clones', ' ', ','); $replace = array('troop1a', 'troop2a', 'troop3a', 'troop1b', 'troop2b', 'troop3b', 'troop1c', 'troop2c', 'troop3c', 'troop1d', 'troop2d', 'troop3d', ' ', ''); $info = str_replace($find, $replace, $info); preg_match_all('/.+ [0-9]+/', $info, $a); foreach ($a[0] as $k => $v) { preg_match_all('/([^0-9]+[0-9]?[^0-9]+) ([0-9 ]+)/', $v, $b); $c[$b[1][0]] = explode(' ',$b[2][0]); } //print_r($c); $fild_for_sum = array('Strike Action:', 'Covert Skill:', 'troop3a'); $sum =0; foreach ($fild_for_sum as $k) $sum += array_sum($c[$k]); echo 'Sum of fileds ', implode(', ', $fild_for_sum), ' is ', $sum; ?> Quote Link to comment https://forums.phpfreaks.com/topic/61059-php-math/#findComment-303929 Share on other sites More sharing options...
cry of war Posted July 21, 2007 Author Share Posted July 21, 2007 that works but do you mind explaining how it does it??? Quote Link to comment https://forums.phpfreaks.com/topic/61059-php-math/#findComment-304111 Share on other sites More sharing options...
sasa Posted July 21, 2007 Share Posted July 21, 2007 preg_match_all('/.+ [0-9]+/', $info, $a); <-- find line with numbers in it preg_match_all('/([^0-9]+[0-9]?[^0-9]+) ([0-9 ]+)/', $v, $b); <-- separate text and numbers part $c[$b[1][0]] = explode(' ',$b[2][0]); <-- create array who has text part for key and numbers array for value try to print_r($c); Quote Link to comment https://forums.phpfreaks.com/topic/61059-php-math/#findComment-304132 Share on other sites More sharing options...
cry of war Posted July 22, 2007 Author Share Posted July 22, 2007 How would i set each of the below using the above code sassa gave me into so that each seperate number below turns into its corsponding integer value bot 0 0 0 Units 0 0 7227 Human Form Units 100000 930000 1478941 Covert Operatives: 1050000 Anti-Covert Operatives: 0 Attack Turns: 633 Unit Production: 4544 bot 0 0 0 $unit1 = 0; $unit2 = 0; $unit3 = 0; Units 0 0 7227 $unit4 = 0; $unit5 = 0; $unit6 = 7227; Human form Units 100000 930000 1478941 $unit7 = 100000; $unit8 = 930000; $unit9 = 1478941; Covert Operatives: 1050000 $unit10 = 1050000; Anti-Covert Operatives: 0 $unit11 = 0; Attack Turns: 633 $AT = 633; Unit Production: 4544 $unitp = 4544; but each one of the numbers after the $unit# can variy because of each person has different stats and i need these to be equal to diff. values because i will be using them for different things Quote Link to comment https://forums.phpfreaks.com/topic/61059-php-math/#findComment-304898 Share on other sites More sharing options...
sasa Posted July 23, 2007 Share Posted July 23, 2007 try <?php $info = 'bot 0 0 0 Units 0 0 7227 Human Form Units 100000 930000 1478941 Covert Operatives: 1050000 Anti-Covert Operatives: 0 Attack Turns: 633 Unit Production: 4544 '; $find = array( ' ', ','); $replace = array(' ', ''); $info = str_replace($find, $replace, $info); preg_match_all('/.+ [0-9]+/', $info, $a); foreach ($a[0] as $k => $v) { preg_match_all('/([^0-9]+) ([0-9 ]+)/', $v, $b); $c[$b[1][0]] = explode(' ',$b[2][0]); } //print_r($c); $fild_for_sum = array('bot', 'Units', 'Human Form Units','Covert Operatives:','Anti-Covert Operatives:'); $u = array(''); foreach ($fild_for_sum as $k) $u = array_merge($u, $c[$k]); for ($i = 1; $i < 12; $i++) { $n = 'unit'.$i; $$n = $u[$i]; } $AT = $c['Attack Turns:'][0]; $unitp = $c['Unit Production:'][0]; echo " unit1 = $unit1, unit2 = $unit2, unit3 = $unit3, unit4 = $unit4, unit5 = $unit5, unit6 = $unit6, unit7 = $unit7, unit8 = $unit8, unit9 = $unit9, unit10 = $unit10, unit11 = $unit11, AT = $AT, unitp = $unitp"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/61059-php-math/#findComment-305138 Share on other sites More sharing options...
cry of war Posted July 23, 2007 Author Share Posted July 23, 2007 ok thats how you get that info now how do you get that infor out of this info??? recon Mission Report Under the cover of night, your 208,000 Covert Ops sneak into the realm of Jungleman . Your Covert Ops move stealthily through the lands of Jungleman undetected. They are able to gather many documents recording the status of weapons, army size and preparedness, and fortifications. Unfortunately, in order to avoid detection, they are not able to provide complete information. Your Chief of Intelligence provides you with the information gathered: Army Size: Unit Type Attackers Defenders Untrained Mercenaries 0 18 0 Soldier 0 0 158550 NID Agent 200 19 66897 Military Stats Strike Action: 24,548,048 Defensive Action 0 Covert Action: 22 AntiCovert Action 22 Covert Skill: 21 Covert Operatives: 0 Anti-Covert Skill: 0 Anti-Covert Operatives: 0 Attack Turns: 3999 Unit Production: 7244 Quote Link to comment https://forums.phpfreaks.com/topic/61059-php-math/#findComment-305367 Share on other sites More sharing options...
sasa Posted July 23, 2007 Share Posted July 23, 2007 <?php $info = 'recon Mission Report Under the cover of night, your 208,000 Covert Ops sneak into the realm of Jungleman . Your Covert Ops move stealthily through the lands of Jungleman undetected. They are able to gather many documents recording the status of weapons, army size and preparedness, and fortifications. Unfortunately, in order to avoid detection, they are not able to provide complete information. Your Chief of Intelligence provides you with the information gathered: Army Size: Unit Type Attackers Defenders Untrained Mercenaries 0 18 0 Soldier 0 0 158550 NID Agent 200 19 66897 Military Stats Strike Action: 24,548,048 Defensive Action 0 Covert Action: 22 AntiCovert Action 22 Covert Skill: 21 Covert Operatives: 0 Anti-Covert Skill: 0 Anti-Covert Operatives: 0 Attack Turns: 3999 Unit Production: 7244'; $find = array( ' ', ','); $replace = array(' ', ''); $info = str_replace($find, $replace, $info); preg_match_all('/.+ [0-9]+/', $info, $a); foreach ($a[0] as $k => $v) { preg_match_all('/([^0-9]+) ([0-9 ]+)/', $v, $b); $c[$b[1][0]] = explode(' ',$b[2][0]); } //print_r($c); //$fild_for_sum = array('bot', 'Units', 'Human Form Units','Covert Operatives:','Anti-Covert Operatives:'); $fild_for_sum = array('Mercenaries', 'Soldier', 'NID Agent','Covert Operatives:','Anti-Covert Operatives:'); $u = array(''); foreach ($fild_for_sum as $k) $u = array_merge($u, $c[$k]); for ($i = 1; $i < 12; $i++) { $n = 'unit'.$i; $$n = $u[$i]; } $AT = $c['Attack Turns:'][0]; $unitp = $c['Unit Production:'][0]; echo " unit1 = $unit1, unit2 = $unit2, unit3 = $unit3, unit4 = $unit4, unit5 = $unit5, unit6 = $unit6, unit7 = $unit7, unit8 = $unit8, unit9 = $unit9, unit10 = $unit10, unit11 = $unit11, AT = $AT, unitp = $unitp"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/61059-php-math/#findComment-305403 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.