Array disappearing in foreach loop


Posted on 16th Feb 2014 07:03 pm by admin

Okay, I have this problem where an array will become undefined in a foreach loop.

Here is the function plus a couple comments:
Code: [Select]function mineResources($nation) {
global $db;
$resources = $nation['resources'];

//Right here $resources would work

$sectors = $db->getSectorList($nation['id']);
foreach($sectors as $sector) {
//In here, $resources is empty
$sectorResources = $sector['resources'];
foreach($sectorResources as $key => $sResource) {
//Does nothing because $resources is empty when it gets here.
$resources[$key] += $sResource;
}
}
$db->updateCountryInfo($nation, array("resources" => $resources));
}
$db is a global array that holds database information. $nation is an array that holds a nation's data. All arrays work except for $resources and even $resources work outside of the foreach loops. Any ideas why this is happening?

No comments posted yet

Your Answer:

Login to answer
241 Like 5 Dislike
Previous forums Next forums
Other forums

An odd assignment statement. Can someone explain this assignment to me?
What purpose is served by the bit of code between the two equal signs in the $installurl set? Is th

MSSQL/PHP
I am tryint to setup a webapplication developed in PHP, Apache2.0 and SQLServer2000 as backend which

need help about
i store the value of my select statement result to an array and stored it to a variable named $fname

Need help with a mail notify function
I was tasked with setting up a site that's been prewritten. The site is basically a form that captur

Interpret Order
Hello all,

I'm wondering if I have this:

Code: $switch = array(
'one' =>

database design
Hi,

I got a page which allow candidate to type in their previous working experience,
I let

Accessing Infotype data in dialog program
Hi All
In Dialog programs attributes I didn't see any logical database field. How can I access

Text to picture Generator
Hello, i have found this script and it works really good^^ But i have one problem, i would like to c

MII Trends - add data onto chart object
Hello,

Can anyone please provide some thoughts on my current requirement:
its pretty

Career Change into SAP
Hai
I am sajesh ,did my diploma in Mechatronics i have a 4 years of experience in

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash