tags:

views:

3956

answers:

3

I am trying to create a multi dimensional array using this syntax:

$x[1] = 'parent';
$x[1][] = 'child';

I get the error: [] operator not supported for strings because it is evaluating the $x[1] as a string as opposed to returning the array so I can append to it.

What is the correct syntax for doing it this way? The overall goal is to create this multidimensional array in an iteration that will append elements to a known index.

The syntax ${$x[1]}[] does not work either.

A: 
$x = array();
$x[1] = array();
$x[1][] = 'child';
Marius
+12  A: 

The parent has to be an array!

$x[1] = array();
$x[1][] = 'child';
Oli
A: 

I think what you want to do is to use $x['parent'] in the end, isn't it ?

So it's not exactly $x = array() but more something like :

$x = array('parent' => array());
$x['parent'][] = 'child';
e-satis