ntan
ntan

Reputation: 2205

How to declare a scalar value and a non-scalar value at the same level of a multidimensional array?

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.

Upvotes: 1

Views: 548

Answers (5)

fearlesstost
fearlesstost

Reputation: 866

The other posters are exactly right - you're overwriting the value of $x[1] with an array. If you want a tree structure that allows internal nodes to be labelled, you're looking at something like a trie:

class Node {
  public $value = null, $children = array();
  public function set($keys, $value) {
    if (empty($keys)) {
      $this->value = $value;
      return;
    }
    $key = array_shift($keys);
    if (!isset($this->children[$key])) {
      $this->children[$key] = new Node();
    }
    $child = $this->children[$key];
    $child->set($keys, $value);
  }
}

$trie = new Node();
$trie->set(array(1), 1);
$trie->set(array(1, 11), 11);
print_r($trie);

Upvotes: 0

deceze
deceze

Reputation: 521933

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( ... )
            )
        )
    )
)

Upvotes: 2

animuson
animuson

Reputation: 54719

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)

Upvotes: 0

Will Vousden
Will Vousden

Reputation: 33348

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.

Upvotes: 0

erisco
erisco

Reputation: 14319

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)

Upvotes: 2

Related Questions