javascript - Use json_encode() with a foreach loop -


i have following code allows me echo product categories in wordpress withing select box:

 $arguments = array(             'number'     => $number,             'orderby'    => $orderby,             'order'      => $order,             'hide_empty' => $hide_empty,             'include'    => $ids  );   $product_cats = get_terms( 'product_cat', $arguments );   foreach ( $product_cats $cat ) :   echo '<option value="' . $cat->term_id . '">' . $cat->name . '</option>';  endforeach; 

i trying use returned values foreach loop within json_encode() can use categories in javascript code.

but cannot figure out way it, can provide guidance?

if use

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

it not work

try in javascript tag;

<script type="text/javascript">     var mycatjson = json.parse('<?php echo json_encode($product_cats); ?>'); </script> 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -