views:

23

answers:

2
<?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  A: 

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.

Alan Geleynse
Thanks so much! I'm freakin over the moon right now.
zeemy23
A: 

You can get the array you're wanting with a lot less work:

<?php

$child_cats = get_categories(array(
  'child_of' => $parent_cat,
  'fields' => 'names'
));

?>
Gipetto