'Console logging array only returning "Array" not array values

I am trying to create two arrays and compare them with array_intersect(). When I try to console.log my variables, it only returns "Array".

Not sure what I am doing wrong here.

// Initialize array for product categories
    $current_cats = array();
    $cat_terms = get_the_terms( $post->ID, 'product_cat' );

    // Get main product category names
    foreach ( $cat_terms as $term ) {
        $product_cat_name = $term->name;
        // Assign category names to array
        $current_cats[] = $product_cat_name;
        echo $product_cat_name;
        echo "<div><br></div>";
    }

    echo "<script>console.log( '$current_cats' );</script>";

    echo "<script>console.log('Current Categories 1: " . $current_cats[0] . "' );</script>";
    echo "<script>console.log('Current Categories 2: " . $current_cats[1] . "' );</script>";
    
    var_dump($current_cats);

When I call $current_cats, I am expecting to get an array back with all of the values. Instead, I am just getting "Array".

I can access each value by calling $current_cats[0].

If I var_dump($current_cats) I am getting all of the expected values.

array (size=4)
  0 => string '69 Mustang A/C Systems' (length=22)
  1 => string 'Complete A/C Systems' (length=20)
  2 => string 'Ford A/C Systems' (length=16)
  3 => string 'Mustang A/C Systems' (length=19)

What am I doing wrong here?



Solution 1:[1]

Because you are trying to use array as string! Try using print_r() instead of echo.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Milad Elyasi