JSON format with For Each and Arrays

Hi Experts,

I currently have this code:

<?php
header("Content-Type: application/json");

// Load Wordpress
include_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php' );


$args = array(
    'orderby'       => 'name', 
    'order'         => 'ASC',
    'hide_empty'    => true, 
    'child_of'      => 29, 
); 



$productSuggestions = array(); 


$brands = get_terms('product_cat', $args);    
foreach ($brands as $brand) { 
	

$args = array(
    'post_type' => 'product',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'product_cat',
            'field' => 'id',
            'terms' => $brand->term_id
        )
    )
);


$products = get_posts( $args );

foreach ( $products as $product ) { 
	
	array_push($productSuggestions,array('value'=>html_entity_decode($product->post_title),'label'=>html_entity_decode($brand->name)));

}     

$json = json_encode($productSuggestions);

echo $json;



}


?>

Open in new window


which separates "each" with an unwanted ][

Is there anyway to restructure this as it doesnt otuput correct json for use with jquery

JSON Output

[{"value":"Messenger Bag - Bicycles","label":"BlueQ"},{"value":"BlueQ Zipper Bag - Bicycles","label":"BlueQ"},{"value":"Shopper Bag - Bicycles","label":"BlueQ"},{"value":"BlueQ Zipper Bag - Bicycle \"SHE HAD TRAVELED FAR\"","label":"BlueQ"},{"value":"Shoulder Tote Bag - Bicycle \"SHE HAD TRAVELED FAR\"","label":"BlueQ"}][{"value":"Messenger Bag - Bicycles","label":"BlueQ"},{"value":"BlueQ Zipper Bag - Bicycles","label":"BlueQ"},{"value":"Shopper Bag - Bicycles","label":"BlueQ"},{"value":"BlueQ Zipper Bag - Bicycle \"SHE HAD TRAVELED FAR\"","label":"BlueQ"},{"value":"Shoulder Tote Bag - Bicycle \"SHE HAD TRAVELED FAR\"","label":"BlueQ"},{"value":"Womens I like to ride my bicycle Mug","label":"Churchill"},{"value":"Women's Bicycle Mug - Vermilion","label":"Churchill"},{"value":"Women's Bicycle Mug - Blue Story","label":"Churchill"},{"value":"Man's Bicycle Mug - I like to ride my bicycle","label":"Churchill"}][{"value":"Messenger Bag - Bicycles","label":"BlueQ"},{"value":"BlueQ Zipper Bag - Bicycles","label":"BlueQ"},{"value":"Shopper Bag - Bicycles","label":"BlueQ"},{"value":"BlueQ Zipper Bag - Bicycle \"SHE HAD TRAVELED FAR\"","label":"BlueQ"},{"value":"Shoulder Tote Bag - Bicycle \"SHE HAD TRAVELED FAR\"","label":"BlueQ"},{"value":"Womens I like to ride my bicycle Mug","label":"Churchill"},{"value":"Women's Bicycle Mug - Vermilion","label":"Churchill"},{"value":"Women's Bicycle Mug - Blue Story","label":"Churchill"},{"value":"Man's Bicycle Mug - I like to ride my bicycle","label":"Churchill"},{"value":"monkii clip","label":"Free Parable"},{"value":"monkii wedge","label":"Free Parable"},{"value":"monkii mono","label":"Free Parable"},{"value":"T2 single wheel bicycle trailer","label":"Free Parable"},{"value":"dom BT Mallet","label":"Free Parable"},{"value":"monkii cleats","label":"Free Parable"}]

Open in new window


Many Thanks in advance for any help!
maccaj51Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gr8gonzoConsultantCommented:
json_encode should always give you valid JSON and your output that you've provided looks fine, except that you're trying to echo out multiple JSON arrays together, which is the problem. Basically, your foreach loop is giving you output like:

[item1, item2, item3][item4, item5, item6]

If you combine the arrays into one big array before json_encode()ing it, you'll be fine.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gr8gonzoConsultantCommented:
Suggested code:

$allBrandArrays = array();
$brands = get_terms('product_cat', $args);    
foreach ($brands as $brand) {
...
$allBrandArrays[] = $productSuggestions;
}

echo json_encode($allBrandArrays);
0
maccaj51Author Commented:
Many thanks... I have the output echo before the last foreach closing bracket
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.