PHP开关/案例json_encode

时间:2012-08-01 16:36:36

标签: php json

我很担心这个。在使用下面的开关/案例时,如果我回答案例1,我的结果如下:

Metal Cover (300)
Plexi (300)
Flat Cover (200)
Paper Cover (250)

当我回显json_encode时,它返回以下内容:

{"300":"Plexi (300)","200":"Flat Cover (200)","250":"Paper Cover (250)"}

为什么我丢失了一行? - 谢谢

代码

$type = 'cover';
    $stt = 1;
    $productId = 81;

        $results = array();
        switch ($type) {
        case "cover":
            $query = mysql_query("SELECT * 
                                    FROM albumcover 
                                    WHERE productId = '{$productId}'
                                ");
            $results[0] ="None";
            switch ($stt){
                case 1:
                    while($row = mysql_fetch_array($query)){
                        echo $results[$row['price2']] = $row['coverupgrade'] . ' (' . $row['price2'] . ')<br>';
                    }
                break;
                case 2:
                    while($row = mysql_fetch_array($query)){
                        $results[$row['price3']] = $row['coverupgrade'] .' (' . $row['price3'] . ')';
                    }
                    break;
                case 3:
                    while($row = mysql_fetch_array($query)){
                        $results[$row['price4']] = $row['coverupgrade'] .' (' . $row['price4'] . ')';
                    }
                    break;
                default :
                    while($row = mysql_fetch_array($query)){
                        $results[$row['price1']] = $row['coverupgrade'].' ('.$row['price1'].')';
                    }
                    break;
            }
            echo json_encode($results);

2 个答案:

答案 0 :(得分:9)

金属盖和Plexi具有相同的键(300)。键必须是唯一的,因此后者会覆盖第一个键。

如果要为单个键存储多个值,则必须使用另一个数组(不确定这是否是您想要的):

$results[$row['priceX']][] = $row['coverupgrade'].' ('.$row['priceX'].')';

JSON编码它将如下所示:

{
  "300":["Metal Cover (300)", "Plexi (300)"],
  "200":["Flat Cover (200)"],
  "250":["Paper Cover (250)"]
}

答案 1 :(得分:2)

由于您使用$row['pricex']的值作为键,您正在丢失一行,并且您有两行价格相同,因此后一个值会覆盖第一行。为了有效地传输数据,您需要更改JSON的结构:

$type = 'cover';
$stt = 1;
$productId = 81;

$results = array();
switch ($type) {
    case "cover":
        $query = mysql_query("
            SELECT * 
            FROM albumcover 
            WHERE productId = '{$productId}'
        ");
        $results[0] ="None";
        switch ($stt){
            case 1:
                while($row = mysql_fetch_array($query)){
                    $results['data'][] = array('price'=>$row['price2'], 'coverupgrade' => $row['coverupgrade'] . ' (' . $row['price2'] . ')<br>';
                }
                break;
            case 2:
                while($row = mysql_fetch_array($query)){
                    $results['data'][] = array('price'=>$row['price3'], 'coverupgrade' => $row['coverupgrade'] . ' (' . $row['price3'] . ')<br>';
                }
                break;
            case 3:
                while($row = mysql_fetch_array($query)){
                    $results['data'][] = array('price'=>$row['price4'], 'coverupgrade' => $row['coverupgrade'] . ' (' . $row['price4'] . ')<br>';
                }
                break;
            default:
                while($row = mysql_fetch_array($query)){
                    $results['data'][] = array('price'=>$row['price1'], 'coverupgrade' => $row['coverupgrade'] . ' (' . $row['price1'] . ')<br>';
                }
                break;
        }
        break;
}
echo json_encode($results);

根据您显示的数据,这应输出:

{
  "0": "none",
  "data": [
    {
      "price": "300",
      "coverupgrade": "Metal Cover (300)"
    },
    {
      "price": "300",
      "coverupgrade": "Plexi (300)"
    },
    {
      "price": "200",
      "coverupgrade": "Flat Cover (200)"
    },
    {
      "price": "250",
      "coverupgrade": "Paper Cover (250)"
    }
  ]
}