...

View Full Version : Simple array problem



Vin0rz
02-14-2006, 04:06 PM
Hey, I had this problem with arrays (can't remember what it was now) which went correct when using array(), but failed when using square-bracket syntax. It was something like this:


$arr['first level']['second level'][1] = 'bla';
$arr['first level']['second level'][2]['11oneone'] = 'foo';
$arr['first level']['second level'][2]['bar'] = false;

Why doesn't that work. and does the following?


$arr = array(
'first level' => array(
'second level' => array(
1 => 'bla',
2 => array(
'11oneone' => 'foo',
'bar' => false
)
)
);

Note that this was by heart, so typos may occur :P

dumpfi
02-14-2006, 05:53 PM
You must create an array before storing a key-value pair in it, e.g:

$arr = array();
$arr['first level'] = array();
$arr['first level']['second level'] = array();
$arr['first level']['second level'][1] = 'bla';
$arr['first level']['second level'][2] = array();
$arr['first level']['second level'][2]['11oneone'] = 'foo';
$arr['first level']['second level'][2]['bar'] = false; dumpfi



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum