views:

69

answers:

4

Hi to all,

i am trying to setup a multidimensional array but my problem is that i can not get the right order from incoming data.

Explain

$x[1][11]=11;
$x[1]=1;

var_dump($x);

In the above code i get only x[1].

To right would be

$x[1]=1;
$x[1][11]=11;

var_dump($x);

But in my case i can dot ensure that x[1] will come first, and x[1][11] will come after.

Is there any way that i can use the first example and get right the array. Keep in mind that the array depth is large.

I am trying to get an array as tree

$x[node]=node data
$x[node][childs]=childs data
etc..

and from incoming data is not sure that node will come first and child second and i am looking for a solution to create the array right

Thanks

+2  A: 

If you set $x[1] to be 1, then it's a number.
If you set $x[1][11] to anything, than $x[1] is an array.
It can't be a number and an array at the same time.

$x = array(
    1 => 1
);

$x = array(
    1 => array(
        11 => 11
    )
);

You'll have to rethink what structure you actually want.


If you really need each node to both have a value and children, you'll have to go with something like this:

array(
    1 => array(
        'value' => 1,
        'children' => array(
            11 => array(
                'value' => 11,
                'children' => array( ... )
            )
        )
    )
)
deceze
+2  A: 

Case One:

// make $x[1] equal to array(11 => 11)
$x[1][11]=11;

// make $x[1] equal to 1
$x[1]=1;

// result, $x[1] is equal to 1

Case Two:

// make $x[1] equal to 1
$x[1]=1;

// make $x[1] equal to array(11 => 11)
$x[1][11]=11;

// result, $x[1] is equal to array(11 => 11)

I do not know what you really want $x[1] to be. I am going to assume you might want this:

// make $x[1] equal to array(1)
$x[1][] = 1;

// append 11, making $x[1] equal to array(1, 11)
$x[1][] = 11;

// result, $x[1] is equal to array(1, 11)

Or you might just want this:

// make $x equal to array(1)
$x[] = 1;

// append 11, making $x equal to array(1, 11)
$x[] = 11;

// result, $x is equal to array(1, 11)
erisco
I agree but its and array tree and i can not get the right tree order so i have to build the array based on the incoming data
ntan
A: 

You can't set $x[1] and $x[1][11] at the same time. Remember that in setting $x[1][11] you're creating an array with an array like array(11 => 11) and assigning that array to $x[1]. What you're trying to do is have both 1 and the array in $x[1], which isn't possible.

Will Vousden
A: 

Your problem is you're redefining it.

$x[1][11]=11; // $x[1] is Array(11 => 11)
$x[1]=1; // $x[1] is int(1)

var_dump($x); // Will output Array(1 => 1)

and with your second example...

$x[1]=1; // $x[1] is int(1)
$x[1][11]=11; // $x[1] is Array(11 => 11)

var_dump($x); // Will output int(1)

I don't know exactly, but I think what you want to be doing is this:

$x[1][1]=1; // $x[1] is Array(1 => 1)
$x[1][11]=11; // $x[1] is Array(1 => 1, 11 => 11)

var_dump($x); // Will output Array(1 => 1, 11 => 11)
animuson