使用array_unique删除wp_user foreach的重复项 - 呈现错误?

时间:2013-11-17 15:39:12

标签: php wordpress foreach taxonomy array-unique

结合分类法和用户角色,我列出了税收“dog”的所有条款,对于每个术语“dog”,列出了与用户个人资料相关联的所有“颜色”。示例:

  • 用户1将meta“Dog:Golden Retriever”和“Color:Yellow”存储在配置文件中。
  • 用户2具有存储有简档的元“狗:金毛猎犬”和“颜色:黑色”。
  • 用户3将meta“Dog:Golden Retriever”和“Color:Black”存储在个人资料中。

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

已修改:错误不再存在。但是,现在 - echo $数组只回声“黑色”,而不是“黄色”或任何其他颜色。

有什么想法吗?

<?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 }?>

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

问题的解决方案,非常感谢@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 } ?>