删除具有array_unique渲染错误的wp_user重复项


Remove duplicates of wp_user foreach with array_unique - rendering error?

结合分类法和用户角色,我列出了税收"狗"的所有术语,对于每个术语"狗",列出了链接到用户配置文件的所有"颜色"。例:

  • 用户 1 将元"狗:金毛猎犬"和"颜色:黄色"与配置文件一起存储。
  • 用户 2 将元"狗:金毛猎犬"和"颜色:黑色"与配置文件一起存储。
  • 用户 3 将元"狗:金毛猎犬"和"颜色:黑色"与配置文件一起存储。

下面的标题 3 标签吐出"术语名称"=在这种情况下是"狗"。在狗下面,应该有一个通过用户元链接到这只狗的"颜色"列表 - IE,黄色,黑色。重要的是"黑色"只出现一次,而不是两次。我正在尝试删除重复项,但出现错误。

已编辑:错误不再存在。但是,现在 - 回声$array只回显"黑色",而不是"黄色"或任何其他颜色。

有什么想法吗?

<?php $terms = get_terms('dogs');
$count = count($terms);
if ( $count > 0 ){ foreach ( $terms as $term ) { ?>
<h3><?php echo $term->name; ?></h3> // Show different "dog" type names 
<div class="listed_dogs_color_names"> 
    // Now search all editor and contributor user profiles for "dog" and "color" user_meta. Color and dog are both taxonomies that are used both with posts and users (user meta)
    // If matches with the "dog" above, list "color" underneath "dog name" in <h3></h3> above.
<?php
$term_parent = $term->parent;
$term_slug = $term->slug;
$editor_query = new WP_User_Query(
    array(
    'role'         => 'editor',
    'meta_key'     => $term_parent, 
    'meta_compare' => '=', 
    'meta_value'   => $term_slug,       
)
);
$editors = $editor_query->get_results();
$contributor_query = new WP_User_Query(
    array(
    'role'         => 'contributor',
    'meta_key'     => $term_parent, 
    'meta_compare' => '=', 
    'meta_value'   => $term_slug,       
)
);
$contribs = $contributor_query->get_results();
$users = array_merge( $contribs, $editors ); 
?>
<?php 
                    $array = array(); // initialize as empty array ?>
                        <?php if (!empty($users)) {?>
                                <?php foreach ($users as $user) : 
                                        $b = $user->color;
                                        $color = explode("'n", $b);
                                        $array[] = $color[0];
                                        ?>
                            <?php endforeach; ?>
                            <?php $array = array_unique($array); ?>
                            <?php 
                            echo "<li>";
                            echo $array[0]; 
                            echo "</li>";
                            ?>
                        <?php } ?>
</div><!--close-->
<?php }?>
<?php }?>

默认情况下,PHP 会尝试将对象转换为字符串进行比较。这会导致错误。您可以指定 array_unique() 的第二个参数来覆盖它:

foreach(array_unique($users, SORT_REGULAR) as $user)

问题的解决方案,非常感谢@Amal和我一起走过它

<?php if (!empty($users)) {?>
  <ul>
    <?php 
   $array = array();
   foreach ($users as $user) :  
       $b = $user->color;
       $color = explode("'n", $b);
       $array[] = $color[0];
    ?>
<?php endforeach; ?> // close the foreach, looped through all users already and stored meta into array
<?php $array = array_unique($array, SORT_REGULAR); ?> // take array and remove dupes                        
     <?php foreach ($array as $item) { ?> // foreach and style. can also implode and separate with commas via <?php $spitarray = implode ( ", ", $spitarray); ?>
          <li><?php echo $item; ?></li>
     <?php } ?>
  </ul>
<?php } ?>