$child = $children_elements[$i];
if ($root->$parent_field == $child->$parent_field ) {
$top_level_elements[] = $child;
array_splice( $children_elements, $i, 1 );
$child = $children_elements[$i];
if ($root->$parent_field == $child->$parent_field ) {
$top_level_elements[] = $child;
array_splice( $children_elements, $i, 1 );