控制台记录数组仅返回“数组”不是数组值

发布于 01-22 10:47 字数 1269 浏览 2 评论 0原文

我正在尝试创建两个数组,并将它们与Array_intersect()进行比较。当我尝试安装我的变量时,它只会返回“数组”。

不知道我在这里做错了什么。

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

当我调用$ current_cats时,我期望将一个数组带回所有值。相反,我只是得到“数组”。

我可以通过调用$ current_cats [0]来访问每个值。

如果我var_dump($ current_cats),我将获得所有预期值。

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)

我在这里做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我闹别瞎闹2025-01-29 10:47:38

因为您试图将数组用作字符串!
尝试使用print_r()而不是echo

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文