Dan
Dan

Reputation: 1244

Create array, and add a bunch of stdCLass objects to it

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;
}

Upvotes: 1

Views: 9878

Answers (2)

jensgram
jensgram

Reputation: 31508

$parentarray = array();
foreach ($otherarray as $bar) {
    $parent = new stdClass;
    $parent->conversion = $bar;
    $parent->negative = $constant;
    $parentarray[] = $parent; // Or am I mistaken?
}

Upvotes: 7

sharpner
sharpner

Reputation: 3937

why not use

$parentarray[] = $parent; 

?

Upvotes: 1

Related Questions