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



我有一个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, "

您的tagsString有语法错误,您不是连接字符串,而只是使用","。这是你的

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

相关内容

  • 没有找到相关文章

最新更新