php重新组合数据 php数组重复
PHP 多个数组合并 重新生成新的数组并按要求值相加
$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)
PHP如何重新组合生成需要的数组?
你分析一下原数组,你看键值0 就不需要改了吧。这样你就直接赋值给新数组$new
然后循环原数组
foreach($old as $key=$val){
//具体的你自己去写正则把,我只是写个大概,把匹配到的剩余 $match[1] 赋值给 $new
if( preg_match(("/^(Content-Type:)/",$val,$match) ){
$new['Content-Type'] = $match[1];
}elseif(preg_match('/^(Server:)/',$val)){
$new['Server'] = $match[1];
}elseif(.....){
依次类推
}
}
var_dump($new);
求段PHP代码,需要将数组重新组合
$arr = [
[
'code' = 'site_name',
'name' = '网站名称',
'value' = '管理系统',
'type' = 'text',
'box' = '',
'tab' = 'main',
'sort' = '1'
],
[
'code' = 'site_title',
'name' = '网站标题',
'value' = '管理系统',
'type' = 'text',
'box' = '',
'tab' = 'main',
'sort' = '2'
],
[
'code' = 'site_keywords',
'name' = '关键词',
'value' = '管理系统',
'type' = 'text',
'box' = '',
'tab' = 'main',
'sort' = '3'
],
[
'code' = 'site_description',
'name' = '站点描述',
'value' = '管理系统',
'type' = 'text',
'box' = '',
'tab' = 'main',
'sort' = '4'
]
];
function merge($arr)
{
$ret = [];
foreach ($arr as $item) {
$ret[$item['code']] = $item['value'];
}
return $ret;
}
print_r(merge($arr));
名称栏目:php重新组合数据 php数组重复
分享路径:http://ybzwz.com/article/dohpepo.html