为什么是php array_unique($ array);产生重复值

时间:2015-10-21 16:02:05

标签: php arrays array-unique

我有一个PHP功能,我试图尽可能高效,但有冗余我无法摆脱。所以我需要一些帮助。

如果你想看一下,这是实际的代码

function array_tags( $classes, $item, $args ){
if ( 'themes' === $args->theme_location ) {
    $tagsString = "";
        //cannot have spaces!!!!! change the page_title for something else
        $cat = get_queried_object();
        $catSlug = $cat->slug;

            $args = array(
                'post_type' => 'product',
                'product_cat' => $catSlug,
                'posts_per_page' => -1,
                );
            $loop = new WP_Query( $args );
            if ( $loop->have_posts() ) {

                while ( $loop->have_posts() ) : $loop->the_post();
                global $product;   

                   $tagsString .=  strip_tags($product->get_tags()) . ", ";    
                endwhile;
            } else {

            }
            wp_reset_postdata(); 


            $arr_tags = explode( ", ", $tagsString ); 

    var_dump(array_unique($arr_tags));
    if( !in_array($item->title, array_unique($arr_tags)) ){
        $classes[] = "not-the-droid-you-are-looking-for";
    }
    else{

    }
}
return $classes;
}
add_filter('nav_menu_css_class', 'array_tags', 10, 3);

这是var_dump($arr_tags);

的输出
array(12) { 
    [0]=> string(20) "Discover your Wonder" 
    [1]=> string(3) "web" 
    [2]=> string(6) "aliens" 
    [3]=> string(9) "astrology" 
    [4]=> string(16) "celestial bodies" 
    [5]=> string(20) "Discover your Wonder" 
    [6]=> string(5) "humor" 
    [7]=> string(9) "mythology" 
    [8]=> string(7) "science" 
    [9]=> string(5) "space" 
    [10]=> string(20) "Discover your Wonder" 
    [11]=> string(0) "" 
} 
array(12) { 
    [0]=> string(20) "Discover your Wonder" 
    [1]=> string(3) "web" 
    [2]=> string(6) "aliens" 
    [3]=> string(9) "astrology" 
    [4]=> string(16) "celestial bodies" 
    [5]=> string(20) "Discover your Wonder" 
    [6]=> string(5) "humor" 
    [7]=> string(9) "mythology" 
    [8]=> string(7) "science" 
    [9]=> string(5) "space" 

等等,为什么$ arr_tags上的var_dump会产生多个数组?

此外,这里是var_dump($tagsString)

string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, "

这是var_dump(strip_tags($product->get_tags()));

的输出
string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, "

这是var_dump((string)strip_tags($product->get_tags()));

的输出
string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, "

1 个答案:

答案 0 :(得分:1)

你的tagsString有语法错误,你没有查询字符串,而只是你正在使用"," 。 这是

的工作代码
<?php
$tagsString = "cats, dogs, dogs, dogs";
$array_tags = explode(", ", $tagsString);
$unique = array_unique($array_tags);
print_r($unique);