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,244
  • 16
  • 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
  • 31,109
  • 6
  • 81
  • 98
1

why not use

$parentarray[] = $parent; 

?

sharpner
  • 3,857
  • 3
  • 19
  • 28