在PHP中将数组输出转换为字符串,然后添加到另一个数组

时间:2014-01-21 19:53:08

标签: php arrays string

我不确定我问的是正确的问题,但这里有......

$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', 
'tag__in' => array (46, 5, 101, 22, 122, 7, 102, 15, 104, 47, 105, 66, 43, 123),
'tag__not_in' => array (comma separated list output by echo $tagString in same format as 'tag__in')
);

有人建议使用爆炸但是当我这样做时它会返回......

Array ( [0] => 10 [1] => 121 [2] => 20 [3] => 36 etc etc)

我需要丢失所有格式,只需获取以逗号分隔的列表。

可能我正在接近这个错误,也许我没有意义,但希望有人能够遵循我想要实现的目标。任何帮助表示赞赏。

这个问题与我这个问题有关...... Trouble including array output in another array

更新

谢谢詹姆斯,他把我放在了正确的位置。这是诀窍的代码......

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

$args = array(
    'post_type' => 'post', 
    'posts_per_page' => 12,
    'paged' => $paged,
    'tag__in' => $includeTags,
    'tag__not_in' => $excludeTags
);

为了清晰起见,我更改了数组名称。

4 个答案:

答案 0 :(得分:1)

对于第2块,你只能使用

$args = array(
'post_type' => 'post', 
'tag__in' => $tagString,
);

答案 1 :(得分:1)

我绝对不确定你在问什么。首先,我对原始阵列的工作原理感到惊讶。您正在使用关联数组和它看起来的增量数组之间的混合。

你可以试试像:

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

exclude本身就是一个数组。然后:

$args = array(
'post_type' => 'post', 
'tag__in' => $excludeArr,
'tag__not_in' => $excludeArr
);

由于$argsin似乎相同,所以not_in数组都不会产生太大的影响。

如果您只需要$excludeArr作为字符串,则可以implode(",", $excludeArr);

修改

在看到您的更新后,我按照要求编辑我的答案,以显示2个单独的数组。

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

$args = array(
    'post_type' => 'post', 
    'posts_per_page' => 12,
    'paged' => $paged,
    'tag__in' => $includeTags,
    'tag__not_in' => $excludeTags
);

答案 2 :(得分:0)

你最好的朋友被命名为

serialize()

http://www.php.net/manual/de/function.serialize.php

这只是将整个数组内容转换为字符串。 :)

答案 3 :(得分:0)

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

$args = array(
'post_type' => 'post', 
'posts_per_page' => 12,
'paged' => $paged,
'tag__in' => $includeTags,
'tag__not_in' => $excludeTags
);