0

Working Code:

$node1 = new stdClass();
$node1->field_granule_comments1['und'][0]['value'] = "test";
print_r($node1); 

Result

stdClass Object
(
    [field_x] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [value] => test
                        )
                )

        )

)

I need output like this but I have value in a variable. For example:

$id="field_x['und'][0]['value']";
$node2 = new stdClass();
$node2->$id ="test";
print_r($node2);

Output of this code is :

stdClass Object
(
    [field_x['und'][0]['value']] => test
)

How can I come up with output similar to "Working Result", taking the value from the variable ?

1 Answer 1

1

Try this:

$id="field_x['und'][0]['value']";
$node2 = new stdClass();
$node2->{$id} ="test";
print_r($node2);

Enclose the $id variable variable within curly brackets.

Sign up to request clarification or add additional context in comments.

4 Comments

I don't see any difference in output. Actually I already tried it before.
what version of php are you running?
I am using php 5.3.13
I don't think it's possible to use multidimensional variable variables to set properties within the class :-s I've been playing around with it and I can set single level variable variables, but not multidimensional ones, I'll have a play around and see if I can update my answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.