麻烦包括另一个数组中的数组输出

时间:2014-01-16 17:48:50

标签: php arrays variables

我有以下代码:

$tags = get_tags(array('exclude' => 46,5,101,22,122,7,102,15,104,47,105,66,43,123, 'fields' => ids));
$tagString = implode (',' , $tags);
echo $tagString;

哪个回声...... 10,121,20,36,23,66,24,21,105,76,82,17,22,122,43,47,102,5,6,106,8,75,54,38,57,86,56,101,123,95,25,62, 16,39,40,69,37,9,42,7,15,41,87,73,85,104

这很棒。但是,我实际上想要将结果(我猜是$ tagString)包含在另一个数组中,如下所示......

$args = array(
    'post_type' => 'post', 
    'posts_per_page' => 12,
    'paged' => $paged,
    'tag__in' => array (46, 5, 101, 22, 122, 7, 102, 15, 104, 47, 105, 66, 43, 123),
    'tag__not_in' => array ($tagString)
);

我尝试删除括号,添加单/双/无引号,删除单词' array'在所有组合的$ tagString前面,但它不起作用。当我手动创建...

'tag__not_in' => array (10,121,20,36,23,24,21,76,82,17,6,106,8,75,54,38,57,86,56,95,25,62,16,39,40,69,37,9,42,41,87,73,85)

代码完美无缺。如何将$ tagString的输出作为' tag__not_in'的内容。括号内的数组?这可能吗?

======

更新以反映Amal的代码......

$tags = get_tags(array('exclude' => 46,5,101,22,122,7,102,15,104,47,105,66,43,123, 'fields' => ids));
$tagString = implode (',' , $tags);
echo $tagString;

$args = array(
    'post_type' => 'post', 
    'posts_per_page' => 12,
    'paged' => $paged,
    'tag__in' => array (46, 5, 101, 22, 122, 7, 102, 15, 104, 47, 105, 66, 43, 123),
    /*'tag__not_in' => array (10,121,20,36,23,24,21,76,82,17,6,106,8,75,54,38,57,86,56,95,25,62,16,39,40,69,37,9,42,41,87,73,85)*/
    'tag__not_in' => explode(',', $tagString)
);

1 个答案:

答案 0 :(得分:3)

$tagString是一个字符串 - 只需将其插入array(...)就不会创建数组(除非您使用eval(),这通常是一个坏主意)。只需使用explode()从逗号分隔的字符串创建一个数组,如下所示:

'tag__not_in' => explode(',', $tagString)