简体   繁体   中英

How to create array of object from values of another variable in php?

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM