duanfei8399 2010-10-26 18:59
浏览 68
已采纳

我如何改变这个wordpress php输出到一个数组?

<?php
$parent_cat = 57;
$child_cats = get_categories('child_of='.$parent_cat);
if($child_cats) :
  echo '{ ';
    foreach($child_cats as $cat) {
    echo $sep . $cat->cat_name;
    $sep = ', ';
    }
  echo ' }';
endif;
?>

The above code outputs a number of categorys in this format:

A Cut Above,A20Labs,AMCH,

how would I add ' ' around each of the elements for output like this?

'A Cut Above','A20Labs','AMCH',

2nd question, how would I code it so that that the output goes into this array code like this?

<?php $type_array = array('A Cut Above','A20Labs','AMCH',)?>

Thanks so much! Azeem

  • 写回答

2条回答 默认 最新

  • douduan9129 2010-10-26 19:04
    关注

    For your first question, change echo $sep . $cat->cat_name; to echo $sep . '\''.$cat->cat_name.'\'';

    This will change it to output the name with single quotes around them.

    To return an array instead, try this:

    <?php
    $parent_cat = 57;
    $child_cats = get_categories('child_of='.$parent_cat);
    $type_array = array();
    if($child_cats) :
        foreach($child_cats as $cat) {
        $type_array[] = $cat->cat_name;
        }
    endif;
    ?>
    

    This will place the names into a new array instead of echoing them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?