Page 2 of 2
Re: 2-dimensional array in PHP?
Posted: Thu Jan 14, 2010 3:45 am
by OldRod
Yeah, I tried all that - been googling it all day and can't seem to come up with what I'm doing wrong.
I've figured out a different way to do what I was trying, but it's maddening I can't figure this out

Re: 2-dimensional array in PHP?
Posted: Thu Jan 14, 2010 4:24 am
by hallsofvallhalla
you need to define the size of the array during initial declaration. Leaving it the way you have it makes it a empty array with no place to put anything.
As a test build the array with random data ob build then replace the data as needed.
Re: 2-dimensional array in PHP?
Posted: Thu Jan 14, 2010 6:05 am
by Jackolantern
hallsofvallhalla wrote:you need to define the size of the array during initial declaration. Leaving it the way you have it makes it a empty array with no place to put anything.
As a test build the array with random data ob build then replace the data as needed.
PHP arrays are dynamic, though. You don't even need to initialize them. You can just start using it in code, and it is created with just enough space to hold the current data the first time the identifier is used. However, they can grow and shrink dynamically to hold more data or delete it. Freaking weird weakly typed languages.
Re: 2-dimensional array in PHP?
Posted: Thu Jan 14, 2010 2:17 pm
by hallsofvallhalla
oh that is interesting, as a rule of them I try to define them from the get go but it seems your right it can be added to as you go. Looks like I have something to study on

Re: 2-dimensional array in PHP?
Posted: Thu Jan 14, 2010 3:46 pm
by Loopy
Are you sure your array is being populated? If you can't display the contents, then something is wrong with the input.
Do one of these:
I use multi arrays in my game as well. Mine looks something like this:
$aMissileEligible[]=array($aTroopType[$a],$attackerArmyNumbers[$a], $aTerrainBonus, $aIndexAmount, $aWeatherBonus);
I don't see any problems with how you're referencing the array, I do it basically the sameway. For me it's usually done through a loop. In this case, I have multiple troops of one troop type (i.e. 50 light infantry archers), I reference any of the particular abilities above by using a loop and something like:
Where $x equals the different troop types within that array, and the 2 refers to the key corresponding to the terrain bonus.
Re: 2-dimensional array in PHP?
Posted: Thu Jan 14, 2010 5:35 pm
by Jackolantern
hallsofvallhalla wrote:oh that is interesting, as a rule of them I try to define them from the get go but it seems your right it can be added to as you go. Looks like I have something to study on

Well, you are usually making it easier on yourself if you define your variables in as few places as possible, use type hinting wherever you can, and intercept incorrect types inside of functions. Weak typing can be neat and useful, but it can also make your code turn into a mangled mess.
