0

I have a php function I'm trying to make efficient as possible, but there are redundancies I cannot get rid of. So I need some help.

Here's the actual code if you would like to take a look

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);

here is the output for 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" 

and so on, why does var_dump on $arr_tags produce multiple arrays?

also, here is the 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, "

here is the output of 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, "

here is the output of 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, "
Chris Haugen
  • 825
  • 1
  • 7
  • 22
  • Or a typo, I guess it should be $tagsString = "cats, dogs, dogs, dogs"; – Chris Haugen Oct 21 '15 at 16:07
  • Have you tried testing a simple string/array instead of an array of 1000 items? – Mr. Meeseeks Oct 21 '15 at 16:08
  • @ChrisHaugen [Even so, can't reproduce your problem](https://eval.in/454888). – al'ein Oct 21 '15 at 16:08
  • Obviously, there is some piece of code missing if your result is that one. If you can't show it entirely, I suggest you review what's going on. – al'ein Oct 21 '15 at 16:11
  • Well, I just replaced the example with the real code, if you'd like to take a gander at it – Chris Haugen Oct 21 '15 at 16:11
  • Instead of `var_dump(array_unique($arr_tags));`, try `var_dump($arr_tags);` and show us a chunk of repeated values. – al'ein Oct 21 '15 at 16:13
  • @PaulCrovella he tried, actually... but couldn't. Check the review historic. – al'ein Oct 21 '15 at 16:13
  • Ok i edited the question. So, the string `$tagsString` get exploded, but why does the exploded array create multiple arrays? shouldn't explode create one single array containing all the string items? – Chris Haugen Oct 21 '15 at 16:20
  • It seems you're trying to manipulate a multidimensional array as a flat array, and might be why `array_unique` is not working. – al'ein Oct 21 '15 at 16:23
  • That's what I'm seeing, however, I think it is caused by the variable `$tagsString` each concatenation to it inside the while loop is creating a new string instead of adding the text to the existing string. Do you know of a way to add on to $tagsString's existing string instead of creating a new string? – Chris Haugen Oct 21 '15 at 16:25
  • [Couldn't reproduce, again](https://eval.in/454900). Your problem is elsewhere. – al'ein Oct 21 '15 at 16:26
  • Yes, you're right. What `strip_tags($product->get_tags())` is returning you? – al'ein Oct 21 '15 at 16:27
  • I'm editing the question with tht info – Chris Haugen Oct 21 '15 at 16:28
  • Which is what you wanted, that's why I've formatted it, to be sure it wasn't the code logic, but pure data fault. Your concatenation doesn't seem wrong, however. Try casting it as string >> `(string)strip_tags($product->get_tags())` – al'ein Oct 21 '15 at 16:29
  • Thank you, for all the help, by the way :) I posted the output you requested – Chris Haugen Oct 21 '15 at 16:30
  • Now, the question would be, how to turn all those strings into a single string, right? – Chris Haugen Oct 21 '15 at 16:31
  • I never saw a single non-object/array variable being set with multiple data – al'ein Oct 21 '15 at 16:32
  • Anyway, try the casting thing I've said, see what you get. `(string)strip_tags($product->get_tags())` add the data type inside round brackets to force its type. – al'ein Oct 21 '15 at 16:34
  • I've edited the comment. – al'ein Oct 21 '15 at 16:39
  • Ok, I did that and added it to the quesiton, it looks exactly the same, though. Also. I tried doing .join to the array to convert it back to a string, it did the same thing. – Chris Haugen Oct 21 '15 at 16:41
  • Do you know what I think you should do? You should open another questions, asking why your **single** variable has **multiple** repeated data, because I couldn't figure it out so far and it seems to be the core of your issue. Use our discussion and your edited question as material. – al'ein Oct 21 '15 at 16:44

1 Answers1

1

Your tagsString has syntax error you are not concating string instead your are just using "," . Here is working code of your

<?php
$tagsString = "cats, dogs, dogs, dogs";
$array_tags = explode(", ", $tagsString);
$unique = array_unique($array_tags);
print_r($unique);
Samundra Khatri
  • 997
  • 1
  • 9
  • 18