This could be simplified to just always use array_slice, dunno if the merge method above is more performant but thats probably a case of premature optimization. I'm assuming children is a numeric array and not keyed by the name which would potentially cause issues here. Also if children is anything but a plain php array it will require iterator_to_array. I havent tested this code yet.