PDA

View Full Version : Array Inside of an Array?


Jaxel
03-13-2009, 05:10 AM
Okay... lets say I have 2 variables...

$names[50] = 'Jaxel'
$players[3] = 50

Shouldn't therefore...

$names[$players[3]] = 'Jaxel' ?

I am trying to do this, but I am getting an error...

Warning: Illegal offset type in [path]

How do I do something like this?

TigerC10
03-13-2009, 07:23 AM
Not quite... I think you want an associative array, as opposed to a nested array.


$players = array(
"Jaxel" => 50,
"Tiger" => 30,
);


You then access like so...
$players["Jaxel"] = 50

Nested arrays (like your example) are matrices. Grids, like checker boards...

[ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ]

You make the vertical variable, and the horizontal variable...

Consider the multiplication tables...
Vertical = X
Horizontal = Y

x=1|[1][2][3][04][05][06][07]
x=2|[2][4][6][08][10][12][14]
x=3|[3][6][9][12][15][18][21]

And you access them like this...

$mutliplicationTable[2][3] == 6


So maybe your example would be better like this
$names[50][3] = 'Jaxel'

But it's pointless to do that if you only have 1 number for each thing. Otherwise you're just wasting a lot of spaces in a row on the table. That's why it might be better (for your example) to use an associative array. Can you dig it?