doddsey_65 Posted November 4, 2011 Share Posted November 4, 2011 This is the array Array (2) ( | ['0'] => Array (6) | ( | | ['name'] = String(7) "Pikemen" | | ['hp'] = Integer(3) 100 | | ['atk'] = Integer(1) 5 | | ['def'] = Integer(1) 5 | | ['rng'] = Integer(2) 15 | | ['amt'] = Integer(2) 30 | ) | ['1'] => Array (6) | ( | | ['name'] = String( "Champion" | | ['hp'] = Integer(3) 150 | | ['atk'] = Integer(1) 8 | | ['def'] = Integer(1) 6 | | ['rng'] = Integer(1) 5 | | ['amt'] = Integer(2) 10 | ) ) I have an attack value ( say 300 ). I want to take this value and minus the first key amount $array[0]['amt']. If that value then comes to 0 I then want to skip to the next array key and do the same $array[1]['amt'] until the attack value runs out or every array keys amount is equal to 0. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/250465-complex-looping/ Share on other sites More sharing options...
doddsey_65 Posted November 4, 2011 Author Share Posted November 4, 2011 Sorry i meant remove the attack value from the hp amount and everytime the hp reaches 0 I need to remove 1 from the amount until it reaches 0, then move on to the next key Quote Link to comment https://forums.phpfreaks.com/topic/250465-complex-looping/#findComment-1285086 Share on other sites More sharing options...
Psycho Posted November 4, 2011 Share Posted November 4, 2011 Not that hard really $attack = 300; foreach($array &$value) { if($value['amt']>=$attack) { //There is enough in this record to subtract all of the attack //Remove the attack amount and exit loop $value['amt'] -= $attack; break; //exit the foreach loop } else { //Remove the amount for this record from the attack and set amount to 0 //Repeat loop for next record $attack-=$value['amt']; $value['amt'] = 0; } } Quote Link to comment https://forums.phpfreaks.com/topic/250465-complex-looping/#findComment-1285087 Share on other sites More sharing options...
doddsey_65 Posted November 4, 2011 Author Share Posted November 4, 2011 but a foreach loop will only loop twice because there is only 2 array keys, i need it to loop until each arra key ['amt'] = 0 or attack = 0 Quote Link to comment https://forums.phpfreaks.com/topic/250465-complex-looping/#findComment-1285091 Share on other sites More sharing options...
Psycho Posted November 4, 2011 Share Posted November 4, 2011 I don't get you. If there are only two records in the array what do you expect to happen after the two records have been processed? The code I provided with process each record one at a time and do the following: 1. If the value of 'amt' is >= the attack, then the attack value id deducted from that record's 'amt' and the script ends. 2. If the value of 'amt' is less than the attack, then that amount is deducted from the attack and the 'amt' for that record is set to 0. The loop then repeats so the remaining attack can be processed against the next record(s) If you want something different you need to provide more details. FYI: There was a small bug in what I provided. Here is the correct line: foreach($array as &$value) Quote Link to comment https://forums.phpfreaks.com/topic/250465-complex-looping/#findComment-1285099 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.