【发布时间】:2016-01-11 09:55:50
【问题描述】:
在从 php for 循环中获取有效 JSON 输出时遇到了一些问题,这是我的 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)"}],
这是我创建 json 输出的 php 循环
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).",";
}
我现在遇到的问题是 JSON 输出现在有效,我不知道如何改进它。
【问题讨论】: