-1
\$\begingroup\$

I have the following arrays:

[costs] => Array (
   [0] => Array (
       [value] => 600
       [name] => Wood
   )
   [1] => Array (
       [value] => 200
       [name] => Stone
   )
   [2] => Array (
       [value] => 800
       [name] => Iron
   )
   [3] => Array (
       [value] => 300
       [name] => Gold
   )
)

Array (
    [0] => Array (
        [balance] => 70636.86
    )
    [1] => Array (
        [balance] => 70636.86
    )
    [2] => Array (
        [balance] => 70636.38
    )
    [3] => Array (
        [balance] => 141072.77
    )
)

I want to check if the second array enough resources to spend from the first array and if one of the values from the first array is not = or less. Can someone help me out?

What I've tried so far:

for($i = 0; $i < 4; $i++)
{
    $balance = $this->listResources[$i]['balance'];
    if($balance >= $this->buildingCosts[$i]['value'])
        $this->listBuildings[$i][$this->listKey]['status'][$balance] = '1';
    else
        $this->listBuildings[$i][$this->listKey]['status'] = '0';
}

What I'm trying to find out is to see if I have enough resources in array to spend ( values that I need are found in costs )

\$\endgroup\$
2
\$\begingroup\$

Your question is extremely cryptic (we can only vaguely guess, for example, that $this->listKey in some way refers to the building type you're trying to create, and some of your sentences are unintelligible), but I suspect you mean something like:

for($i = 0; $i < 4; $i++) {
    $balance = $this->listResources[$i]['balance'];
    $this->listBuildings[$this->listKey]['status'][$i] = ($balance >= $this->buildingCosts[$i]['value']);
}

That populates $this->listBuildings with status arrays that map resource index numbers to booleans indicating whether or not the available quantity of that resource is sufficient.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ thanks it works, i was in a hurry when i posted this and very angry because it didn't work the way i wanted :-) \$\endgroup\$ – Bogdan May 23 '12 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.