当我在html中使用这样的脚本工作正常
{category: "Auto", measure: 6600},
{category: "Best Car", measure: 22200},
{category: "Car Auction", measure: 5400},
{category: "Car Audio", measure: 60500},
{category: "Car Battery", measure: 6600},
但是当我尝试使用像这样的
打印时<?php foreach($rows as $row){ ?>
{category: "<?php echo $row['ad_group']; ?>", measure: <?php echo $row['volume']; ?>},
<?php } ?>
它不起作用。
答案 0 :(得分:2)
您可以将所有数据放入$list
,然后使用json_encode()
对其进行编码。这也将确保您的值被正确转义。
<?php
$list = array();
foreach ($rows as $row)
{
$data = array();
$data['category'] = $row['ad_group'];
$data['measure'] = $row['volume'];
$list[] = $data;
}
echo json_encode($list);
?>