脚本不能用php打印

时间:2014-01-17 09:23:47

标签: php html

当我在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 } ?>

它不起作用。

1 个答案:

答案 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);
?>

另请参阅:http://php.net/json_encode