Having some trouble getting a valid JSON output from a php for loop, here is my JSON:
[{"title":"One Colour ($2.45)","price":"($2.45)"},{"title":"Two Colours ($3.35)","price":"($3.35)"},{"title":"Three Colours ($4.25)","price":"($4.25)"}],[{"title":"One Colour ($2.45)","price":"($2.45)"},{"title":"Two Colours ($3.35)","price":"($3.35)"},{"title":"Three Colours ($4.25)","price":"($4.25)"},{"title":"One Colour ($3.05)","price":"($3.05)"},{"title":"Two Colours ($4.35)","price":"($4.35)"},{"title":"Three Colours ($5.75)","price":"($5.75)"}],
And here is my php loop that creates the json output
foreach ( $product_addons as $addon ) {
foreach ( $addon['options'] as $option ) :
$loop ++;
switch ($qty) {
case ($qty < 20):
$price = $option['price'] > 0 ? ' (' . wc_price( get_product_addon_price_for_display( $option['price'] ) ) . ')' : '';
$title = strip_tags($option['label']. $price);
break;
case ($qty > 20 && $qty < 35):
$price = $option['discount'] > 0 ? ' (' . wc_price( get_product_addon_price_for_display( $option['discount'] ) ) . ')' : '';
$title = strip_tags($option['label']. $price);
break;
}
$select_text[] = array(
'title' => trim($title),
'price' => trim(strip_tags($price)),
);
endforeach;
echo json_encode($select_text).",";
}
The problem I am getting now is that the JSON output is now valid and I cant quite figure out how to improve it.