Use json_encode() with a foreach loop

admin

Administrator
Staff member
I have the following code which allows me to echo my product categories in wordpress withing a select box:

Code:
 $arguments = array(
            'number'     => $number,
            'orderby'    => $orderby,
            'order'      => $order,
            'hide_empty' => $hide_empty,
            'include'    => $ids
 );

 $product_cats = get_terms( 'product_cat', $arguments );

 foreach ( $product_cats as $cat ) :
  echo '<option value="' . $cat->term_id . '">' . $cat->name . '</option>';
 endforeach;

I am trying to use the returned values from my foreach loop within the json_encode() so that I can use the categories in my javascript code.

But I cannot figure out a way to do it, can anyone provide any guidance?

If i use

Code:
<?php echo json_encode($product_cats); ?>

it does not work