最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Remove duplicates - array_unique()

programmeradmin1浏览0评论

I am trying to make a simple list of names that link to profile information. Some of the people in the list have multiple addresses and therefore appear multiple times in the list (as their addresses will appear on the profile page). I would like to remove the duplicate names. I tried using array_unique() but it doesn't seem to have any effect, the duplicates still show. Maybe I'm not using it in the right place? I would appreciate the help. Note: using ACF Pro, hence the use of "the_field()" and "get_field()". Here's my current code:

<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<div id="ipn-in-office-section" class="et_pb_section et_pb_section_1 et_section_regular et_section_transparent">
    <div class="et_pb_row et_pb_row_1 ipn-by-county-row">
        <div class="et_pb_column et_pb_column_3_4 et_pb_column_2    et_pb_css_mix_blend_mode_passthrough et-last-child">
            <div class="et_pb_module et_pb_text et_pb_text_2 et_pb_bg_layout_light  et_pb_text_align_left">
                    <?php   
                                $io_name = $_GET['io_name'];
                                $posts = get_posts(array(
                                'posts_per_page'    => -1,
                                'post_type'         => 'page',
                                'meta_key'          => 'io_name',
                                'orderby'           => 'meta_value',
                                'order'             =>  'ASC'
                            ));
                            if( $posts ):
                            foreach( array_unique($posts, SORT_REGULAR) as $post ): 
                                    setup_postdata( $post );
                    ?>
                    <?php if (get_field('io_name')) { ?>
                                    <div class="ipn-in-office-listing">
                                        <p>
                                        <a href="/in-office-services/by-provider-name?io_name=<?php the_field('io_name'); ?>" target="_parent"><strong style="color: #1068a5;"><?php the_field('io_name'); ?></strong></a> .
                                        </p>
                                    </div>
                                <?php } ?>
                            <?php endforeach; ?>
                            <?php wp_reset_postdata(); ?>
                        <?php endif; ?> 
<?php endwhile; ?>
            </div> <!-- .et_pb_text -->
        </div> <!-- .et_pb_column -->
    </div> <!-- .et_pb_row -->
</div>

I am trying to make a simple list of names that link to profile information. Some of the people in the list have multiple addresses and therefore appear multiple times in the list (as their addresses will appear on the profile page). I would like to remove the duplicate names. I tried using array_unique() but it doesn't seem to have any effect, the duplicates still show. Maybe I'm not using it in the right place? I would appreciate the help. Note: using ACF Pro, hence the use of "the_field()" and "get_field()". Here's my current code:

<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<div id="ipn-in-office-section" class="et_pb_section et_pb_section_1 et_section_regular et_section_transparent">
    <div class="et_pb_row et_pb_row_1 ipn-by-county-row">
        <div class="et_pb_column et_pb_column_3_4 et_pb_column_2    et_pb_css_mix_blend_mode_passthrough et-last-child">
            <div class="et_pb_module et_pb_text et_pb_text_2 et_pb_bg_layout_light  et_pb_text_align_left">
                    <?php   
                                $io_name = $_GET['io_name'];
                                $posts = get_posts(array(
                                'posts_per_page'    => -1,
                                'post_type'         => 'page',
                                'meta_key'          => 'io_name',
                                'orderby'           => 'meta_value',
                                'order'             =>  'ASC'
                            ));
                            if( $posts ):
                            foreach( array_unique($posts, SORT_REGULAR) as $post ): 
                                    setup_postdata( $post );
                    ?>
                    <?php if (get_field('io_name')) { ?>
                                    <div class="ipn-in-office-listing">
                                        <p>
                                        <a href="/in-office-services/by-provider-name?io_name=<?php the_field('io_name'); ?>" target="_parent"><strong style="color: #1068a5;"><?php the_field('io_name'); ?></strong></a> .
                                        </p>
                                    </div>
                                <?php } ?>
                            <?php endforeach; ?>
                            <?php wp_reset_postdata(); ?>
                        <?php endif; ?> 
<?php endwhile; ?>
            </div> <!-- .et_pb_text -->
        </div> <!-- .et_pb_column -->
    </div> <!-- .et_pb_row -->
</div>
Share Improve this question asked Oct 23, 2018 at 16:36 MarkMark 213 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I was able to answer my question thanks to : https://wpquestions/Remove_duplicates_from_ACF_query/12685

<?php
    $posts = get_posts(array(
    'numberposts' => -1,
    'post_type' => 'page',
    'orderby' => 'meta_value',
    'order' => 'ASC',
    ));
if($posts)
    { /* FIRST, get all the post data */
        foreach($posts as $post)
    { /* SECOND, one-by-one, add each data row for the specific field into an array */
        $io_names[] = get_field('io_name');
    } /* THIRD, run through the array and remove all duplicates */
        $io_names = array_unique($io_names);
        foreach($io_names as $io_name)
            { /* FINALLY, display the data on the page  */
            echo '<p><a href="#">' . $io_name . '</a></p>';
            }   
    }
?>
发布评论

评论列表(0)

  1. 暂无评论