Variable in Array Not Working

时间:2016-10-17 作者:aido14

我在为查询中的一个参数使用变量时遇到了一个问题。我使用自定义帖子类型和类别名称作为术语来确定显示哪些类别。

当我将值硬编码到术语中时,效果很好,但当我使用变量时,它似乎不起作用。

This code works:

$args = array(
    \'post_type\' => \'sparknz\',
    \'tax_query\' => array(
        array(
            \'taxonomy\'  => \'sparknz_gardens\',
            \'field\'     => \'name\',
            \'terms\'     => array ( \'The A Team\', \'The B Team\', \'The C Team\' ),
        )
    )
);  

But this does not (notice variable in terms):

$my_term_names = "\'The A Team\',\'The B Team\',\'The C Team\'";

$args = array(
    \'post_type\' => \'sparknz\',
    \'tax_query\' => array(
        array(
            \'taxonomy\'  => \'sparknz_gardens\',
            \'field\'     => \'name\',
            \'terms\'     => array( $my_term_names ),
        )
    )
);
我需要这些术语是一个变量。有什么想法吗?

Just an update as to why I am using a string as a variable. I am using array_intersect to pick out similarities in two arrays:

$my_user_array  = array( "c" => $user_array );
$my_cat_array   = array( "d" => $category_array );
$myresult       = array_intersect( $my_user_array, $my_cat_array );
$my_term_names  = implode( ",", $myresult );

echo $$my_term_names;
不确定是否有其他方法可以做到这一点?

2 个回复
SO网友:Michael Ecklund

以下代码表示为一个连续字符串:

$my_term_names = "\'The A Team\',\'The B Team\',\'The C Team\'";

您希望这些术语是一个数组:

$my_term_names = array( \'The A Team\', \'The B Team\', \'The C Team\' );

现在您需要删除array() 从您的术语查询。

$args = array(
    \'post_type\' => \'sparknz\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'sparknz_gardens\',    // taxonomy name
            \'field\'    => \'name\',               // term_id, slug or name
            \'terms\'    => $my_term_names,       // Terms array (defined above)
        )
    )
);

Edit. 10/19/2016

我知道您想使用array_intersect(); 然而,一旦找到了相似之处,就需要停止尝试将所有内容恢复为字符串格式。array_intersect(); 获取多个数组,查找相似性,并返回一个新数组,其中包含指定的多个数组之间的相似值。

Example #1 array_intersect() example

$array1 = array( "a" => "green", "red", "blue" );
$array2 = array( "b" => "green", "yellow", "red" );
$result = array_intersect( $array1, $array2 );
print_r( $result );

The above example will output:

Array
(
    [a] => green
    [0] => red
)
如果您计划从tax_query, 这个terms 的参数tax_query 需要加入array format . 非字符串格式。

Here\'s an updated example for your situation. I\'ve used simple variable names to get the point across:

$terms_array_1 = array( \'The B Team\', \'The D Team\', \'The A Team\' );
$terms_array_2 = array( \'The A Team\', \'The B Team\', \'The C Team\' );

$similar_terms = array_intersect( $terms_array_1, $terms_array_2 );

print_r( $similar_terms );

$args = array(
    \'post_type\' => \'sparknz\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'sparknz_gardens\',    // taxonomy name
            \'field\'    => \'name\',               // term_id, slug or name
            \'terms\'    => $similar_terms,       // Term Names array (similarities between array1 and array2)
        )
    )
);

SO网友:Howdy_McGee

PHP对这些数组的解释不同。在第一个示例中,您要求WP\\u Query获取帖子
by 3 术语slug,在第二个示例中,为数组赋值为1字符串。如果我们要打印这两个数组,它将如下所示:

Your Example 1:

\'terms\' => array(
    [0] => \'The A Team\',
    [1] => \'The B Team\',
    [2] => \'The C Team\'
)

Your Example 2:

\'terms\' => array(
    [0] => \'\\\'The A Team\',\'The B Team\',\'The C Team\\\'\'
)
PHP认为您给它分配了一个字符串,即使字符串本身中有逗号,它也是一个绝对字符串。您需要做的是使变量本身成为一个数组。

$my_term_names = array(
    \'The A Team\',
    \'The B Team\',
    \'The C Team\'
)

\'terms\' => $my_term_names

相关推荐

检查GET_TERMS请求中是否存在插件

我想让用户能够从前端创建各种自定义帖子,但根据指定的分类,返回链接到它的自定义帖子表单。因此,在获得所选分类法之后,我无法检查get\\u terms()请求中是否存在该分类法我认为这是因为in\\u array()函数在多维数组方面做得不好。因此,我在这里搜索并找到了一种方法来生成另一个克服该问题的函数,但它仍然不起作用这是我的代码:<?php $cptTax = $_GET[\'choosetax\']; $tax1List = get_terms([