duanjia7607 2018-10-12 15:01
浏览 78
已采纳

仅打印数组值 - PHP

I have the following code within a WP loop

<?php
                // Get the selected taxonomies for the post type
                $terms = get_the_terms( get_the_ID(), 'course_type' );
                $classes = array();
                if ( $terms && ! is_wp_error( $terms ) ){
                    foreach ( $terms as $term) {
                        $classes[] = $term->slug;
                    }
                }
                ?>

What I am looking to do is print only the WP_Term object slug to use in the classes.

<div class="courses-search-result standout-box-shadow <?php print_r( $classes ); ?>">

Now, I have got it working to a point and printing a value in the classes - but it is also printing the array structure:

class="courses-search-result standout-box-shadow cilex-clearfix Array
(
    [0] => apprenticeship
)
"

Am I close to getting the correct code with my PHP? My knowledge of the language is basic right now so any help would be much appreciated.

  • 写回答

2条回答 默认 最新

  • dongxiang3648 2018-10-12 15:05
    关注

    print_r is a function intended for debugging, not formatted output.

    The simplest way to achieve what you want is using implode, which combines the values of an array into a string; and then echo to display the string.

    <div class="courses-search-result standout-box-shadow <?php echo implode(' ', $classes); ?>">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘