Create array, and add a bunch of stdCLass objects

2019-06-06 07:11发布

Struggling with the basics here.

I want to setup an array, and fill it with multiple data objects, by looping through an array.

How do I add multiple $parent data objects that are unique, to the parentarray?

When I use the below the last one overwrites all the others. As simple as adding $parent[] or something like that?

Thanks,

$parentarray = array();
foreach ($otherarray as $bar) {
$parent = new stdClass;
$parent->conversion = $bar;
$parent->negative = $constant;
}

2条回答
劳资没心,怎么记你
2楼-- · 2019-06-06 08:14
$parentarray = array();
foreach ($otherarray as $bar) {
    $parent = new stdClass;
    $parent->conversion = $bar;
    $parent->negative = $constant;
    $parentarray[] = $parent; // Or am I mistaken?
}
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-06-06 08:16

why not use

$parentarray[] = $parent; 

?

查看更多
登录 后发表回答