dqhnp44220 2014-01-21 19:53
浏览 190
已采纳

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

I'm not certain I'm asking the right question but here goes...

$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;

When I echo this out via the last line it gives me a lovely list of comma separated numbers (the right numbers too fortunately). However, I don't want to echo them I want to include them in more code as follows...

$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')
);

It has been suggested using explode but when I do that it returns...

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

I need to lose all the formatting and just get the comma separated list.

Possibly I'm approaching this wrong and maybe I'm not making sense but hopefully someone can follow what I'm trying to achieve. Any help appreciated.

It is linked to this question which got me this far... Trouble including array output in another array

Update:

Thank you James who put me on the right lines. This is the code that did the trick...

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

I've changed the array names for clarity.

  • 写回答

4条回答 默认 最新

  • doudan5136 2014-01-21 20:02
    关注

    I'm definitely not sure what you are asking. First, I'm surprised your original array works at all. You are using a mix between an associative array and just an incremental array it looks like.

    Can you try something like:

    $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;
    

    Where the exclude is an array itself. Then:

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

    None of that $args array makes much since since the in and not_in appear to be the same, though.

    If you just need $excludeArras a string, you can implode(",", $excludeArr);

    EDIT

    After seeing your update, I'm editing my answer as requested to show the 2 seperate arrays.

    $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
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 github符合条件20分钟秒到账,github空投 提供github账号可兑换💰感兴趣的可以找我交流一下
  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?