重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
array_unshift(array,value1,value2,value3...)
创新互联公司是一家专注于网站制作、网站建设与策划设计,南澳网站建设哪家好?创新互联公司做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:南澳等地区。南澳做网站价格咨询:028-86922220
函数在数组开头插入一个或多个元素。
$arr = array();
arrray_unshift($arr,$str1,$str2,$str3);
$aa='[{"id": 0, "num": "2", "name": "产品1", "price": "37", "amount": 74, "zdamount": 100}, {"id": 1, "num": "2", "name": "产品2", "price": "37", "amount": 74, "zdamount": 100}]';
$bb='[{"id": 0, "num": "3", "name": "产品1", "price": "37", "amount": 90, "zdamount": 110}, {"id": 1, "num": "5", "name": "产品2", "price": "37", "amount": 150, "zdamount": 180}]';
$cc='[{"id": 2, "num": "3", "name": "产品3", "price": "37", "amount": 90, "zdamount": 110}, {"id": 1, "num": "5", "name": "产品2", "price": "37", "amount": 150, "zdamount": 180}]';
$j_a = json_decode($aa,1);
$j_b = json_decode($bb,1);
$j_c = json_decode($cc,1);
$arr_list = [$j_a,$j_b,$j_c];//把数组扔进大数组,这样可以随意增加个数
$result = [];
foreach ($arr_list as $k=$v) {//第一次循环,把原数组取出
foreach ($v as $key = $value) {//第二次循环,正式取出数据
if (isset($result[$value['id']])) {//如果有此id,则进行累加操作
$result[$value['id']]['num'] += $value['num'];
$result[$value['id']]['amount'] += $value['amount'];
$result[$value['id']]['zdamount'] += $value['zdamount'];
} else {//没有此id则进行赋值操作
$result[$value['id']]['id'] = $value['id'];
$result[$value['id']]['price'] = $value['price'];
$result[$value['id']]['name'] = $value['name'];
$result[$value['id']]['num'] = $value['num'];
$result[$value['id']]['amount'] = $value['amount'];
$result[$value['id']]['zdamount'] = $value['zdamount'];
}
}
}
echo json_encode($result,320);//输出结果,如果需要序列化键值,可以使用array_values($result)
两个方法
1,在sql语句里写个CONCAT(b,c,d,e,f.....) as tt,然后php读取tt就可以。
2,php循环输出的时候定义个变量保存...