我有这个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"}]}
我的问题是,如何合并或只是以编程方式覆盖JSON与'相同'ord_num,customer_id和ord_name
将更新的字段为qty = 7,price_x_quan
我想要的:瓶中的Nestea将有qty = 7,price_x_quan = 105
这是我的orderhow代码
<?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;
?>
请帮忙!
答案 0 :(得分:0)
以上:
$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';
}
答案 1 :(得分:0)
您可以使用带有GROUP BY
子句的SUM函数在MySQL查询中执行此操作。
作为旁注,您应该考虑使用mysqli(尾随 i 代表改进)或PDO来访问您的数据库,而不是deprecated mysql接口。 Now why on earth should I bother to do that?
$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"
}
]
}