控制台记录数组仅返回“数组”不是数组值
我正在尝试创建两个数组,并将它们与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 技术交流群。

因为您试图将数组用作字符串!
尝试使用
print_r()
而不是echo
。Because you are trying to use array as string!
Try using
print_r()
instead ofecho
.