I’m finding it difficult to understand your above code.
Perhaps you will find the issue with your code and solve it by simplifying it by breaking it down into steps.
Please add comments to each step to describe what you are doing along the way.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users