1

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;
}
Dan
  • 1,246
  • 15
  • 37

2 Answers2

7
$parentarray = array();
foreach ($otherarray as $bar) {
    $parent = new stdClass;
    $parent->conversion = $bar;
    $parent->negative = $constant;
    $parentarray[] = $parent; // Or am I mistaken?
}
jensgram
  • 30,081
  • 6
  • 79
  • 95
1

why not use

$parentarray[] = $parent; 

?

sharpner
  • 3,719
  • 3
  • 18
  • 27