排序生成单个html列表的两个不同的php循环

Using ACF plugin in Wordpress I created a checkbox field which return the ID of each selected values. I then echo the image of each selected values through wp_get_attachment_image.

I also created a text field for dynamic input of text like this which then becomes this

Here is a piece of my code :

<?php $values_caract = get_field('picto_statique_Caract');
                            if( !empty($values_caract) ): ?>
                                <div class="icon-list">
                                    <ul>
                                        <?php foreach($values_caract as $value_caract): ?>
                                            <li><?php echo wp_get_attachment_image( $value_caract, 'full' );?></li>
                                        <?php endforeach; ?>
                                        <?php if( get_field('ip3x') ): ?>
                                            <li>
                                                <div class="picto">
                                                    <?php echo wp_get_attachment_image( 3145, 'full' );?>
                                                    <div class="text-ip3x">
                                                        <?php the_field('ip3x'); ?>
                                                    </div>
                                                </div>
                                            </li>
                                        <?php endif; ?>
                                        <?php if( get_field('ip4x') ): ?>
                                            <li>
                                                <div class="picto">
                                                    <?php echo wp_get_attachment_image( 3158, 'full' );?>
                                                    <div class="text-ip3x">
                                                        <?php the_field('ip4x'); ?>
                                                    </div>
                                                </div>
                                            </li>
                                        <?php endif; ?>
     </ul>
     </div>
                            <?php endif; ?>

My issue is that this generate a list of images which I cannot sort because the foreach loop for static images and the if condition for dynamic images are two different loops. Any idea how to do this ?