I've figured out a different way to do what I was trying, but it's maddening I can't figure this out
2-dimensional array in PHP?
Re: 2-dimensional array in PHP?
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
I've figured out a different way to do what I was trying, but it's maddening I can't figure this out
- hallsofvallhalla
- Site Admin
- Posts: 12026
- Joined: Wed Apr 22, 2009 11:29 pm
Re: 2-dimensional array in PHP?
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.
As a test build the array with random data ob build then replace the data as needed.
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: 2-dimensional array in PHP?
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.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.
The indelible lord of tl;dr
- hallsofvallhalla
- Site Admin
- Posts: 12026
- Joined: Wed Apr 22, 2009 11:29 pm
Re: 2-dimensional array in PHP?
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?
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.
Do one of these:
Code: Select all
print_r ($arrayName);
$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];
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: 2-dimensional array in PHP?
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.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
The indelible lord of tl;dr