JSON PHP合并相同插入的数据


JSON PHP Merge Same Inserted Data

我有一个JSON,它具有相同的ord_name、cust_id、ord_num

{"orders":[{"id":"1","ord_name":"Nestea Bottle","ord_desc":"Nestea in a bottle","ord_price":"15","ord_qty":"2","customer_id":"54feec24bff73","ord_num":"13211554feec24bff73","price_x_quan":"30.00","image":"http://192.168.43.52/MMOS/uploads/nestea_bottled.jpg","subtotal":"","imgimg":"uploads/nestea_bottled.jpg"},{"id":"2","ord_name":"Nestea Bottle","ord_desc":"Nestea in a bottle","ord_price":"15","ord_qty":"4","customer_id":"54feec24bff73","ord_num":"13211554feec24bff73","price_x_quan":"60.00","image":"http://192.168.43.52/MMOS/uploads/nestea_bottled.jpg","subtotal":"","imgimg":"uploads/nestea_bottled.jpg"},{"id":"3","ord_name":"Nestea Bottle","ord_desc":"Nestea in a bottle","ord_price":"15","ord_qty":"1","customer_id":"54feec24bff73","ord_num":"13211554feec24bff73","price_x_quan":"15.00","image":"http://192.168.43.52/MMOS/uploads/nestea_bottled.jpg","subtotal":"","imgimg":"uploads/nestea_bottled.jpg"}],"o_total":[{"total":"105"}]} 

我的问题是,如何用"相同"的ord_num、customer_id和ord_name 以编程方式合并或覆盖JSON

将更新的字段为数量=7,价格_x_quan

我想要的:雀巢在一个瓶子里的数量=7,价格_x_quan=105

这是我的订单显示代码

    <?php
mysql_connect('localhost','root','')or die ('No Connection');
mysql_select_db('dbmoms');
//$ord  = $arr['ord_num']
$sum=0;
$total = $sum;

$sql1 ="SELECT * FROM orders ORDER BY id desc  LIMIT 1"; 
if($row=mysql_fetch_array(mysql_query($sql1))){
    $order_id=$row['ord_num'];
}
$sql ="SELECT * FROM orders  WHERE ord_num = '$order_id' "; 

$result = mysql_query($sql);
$arr["orders"] = array();
    while($row = mysql_fetch_assoc($result)){
    $arr['orders'][]= $row ;
    $sum = $sum+$row['price_x_quan'];


}
$arr['o_total'][] = array('total' => "$sum" );

$json_encoded_string = json_encode($arr); 
 $json_encoded_string = str_replace("''/", '/', $json_encoded_string);
echo $json_encoded_string;

?>

请帮忙!

以上:

$arr['orders'][]= $row ;

看跌:

// Specify custom values for Nestea..
if( $row[ 'ord_desc' ] == 'Nestea in a bottle' )
{
    $row[ 'ord_qty' ] = '7';
    $row[ 'price_x_quan' ] = '105.00';

}

您可以在MySQL查询中使用带有GROUP BY子句的SUM函数来完成此操作。

附带说明一下,您应该考虑使用mysqli(后面的i代表改进的)或PDO来访问数据库,而不是不推荐使用的mysql接口。现在,我究竟为什么要这么做呢?

$sql1 ="SELECT * FROM orders ORDER BY id desc  LIMIT 1";
if($row=mysql_fetch_array(mysql_query($sql1))){
    $order_id=$row['ord_num'];
}
// Use SUM and GROUP BY to let the database do the math, introducing
// the calculated 'sum_price_x_quan' and 'sum_ord_qty' columns
$sql = "
    SELECT    ord_num,
              ord_desc,
              sum(price_x_quan) as sum_price_x_quan,
              sum(ord_qty)      as sum_rod_qty
    FROM      orders
    WHERE     ord_num = '$order_id'
    GROUP BY  ord_num
";
$result = mysql_query($sql);
$arr = array();
if ($row = mysql_fetch_assoc($result)) {
    $arr['orders'][] = $row ;
    $arr['o_total'][] = array('total' => $row["sum_price_x_quan"]);
}
$json_encoded_string = json_encode($arr);
echo $json_encoded_string;

输出:

{
    "orders":  [
        {
            "ord_num":          "13211554feec24bff73",
            "ord_desc":         "Nestea in a bottle",
            "sum_price_x_quan": "105.00",
            "sum_ord_qty":      "7"
        }
    ],
    "o_total": [
        {
            "total": "105.00"
        }
    ]
}