2-dimensional array in PHP?

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
User avatar
OldRod
Posts: 1320
Joined: Sun Sep 20, 2009 4:26 pm

Re: 2-dimensional array in PHP?

Post 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 :)
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: 2-dimensional array in PHP?

Post 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.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: 2-dimensional array in PHP?

Post 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.
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: 2-dimensional array in PHP?

Post 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 ;)
Loopy

Re: 2-dimensional array in PHP?

Post 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:

Code: Select all

print_r ($arrayName);
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:

Code: Select all

$aMissileEligible[$x][2];
Where $x equals the different troop types within that array, and the 2 refers to the key corresponding to the terrain bonus.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: 2-dimensional array in PHP?

Post 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. :o
The indelible lord of tl;dr
Post Reply

Return to “Advanced Help and Support”