Hello,
On https://www.phpdocx.com/documentation/cookbook/clone-blocks you can read how to work with blocks. Also please check the included samples in the package.
On https://www.phpdocx.com/documentation/snippets/clone-blocks-replacing-placeholders is explained how to clone blocks and replace placeholders.
About your code, the problem is that you are cloning and replacing its content. We recommend you to do it using two loops:
foreach ($names as $key => $item) {
$docx->cloneBlock('NAME');
}
foreach ($names as $key => $item) {
$docx->replaceVariableByHtml('name', 'block', $item);
}
And use the firstMatch option available in the replace methods.
Anyway, we recommend you to upgrade to phpdocx 8.2 that includes many improvements to work with blocks. If you upgrade to this version, feel free to send to contact[at]phpdocx your template and the dev team will generate a custom code to illustrate how to do it.
Regards.