Jump to content

Complex Looping


doddsey_65

Recommended Posts

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?

 

Link to comment
Share on other sites

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;
    }
}

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.