Create array from nested structure

I have a strcuture field inside a structure field that im trying to loop over in a content representation to turn into json array. What i cant figure out is how to stop the in loop getting added to the previous item in the array. The below works but the second item contains the first and second items etc, because i think the outer loop is causing the inner one to keep adding to the arrary.

How can i get the inner structure field without this happening?

foreach ($site->food()->toStructure() as $food) {
  foreach ($food->menutypes()->toStructure() as $menutype) {
    $menutypes[] = array(
      'title' => $menutype->mainmenu()->toLinkObject()->title(),
      'barba' => false,
      'link' =>  $menutype->mainmenu()->toLinkObject()->value(),

  $menulinks[] = array(
    'title' => $food->label()->value(),
    'children' => $menutypes,

$menu = $menulinks;

If you reset $menutypes to an empty array before the second loop, you don’t run into this problem

ah, pefect, thanks @texnixe