欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP数组合并重组问题

程序员文章站 2022-06-05 16:21:57
...
我有这样一个数组
[1] => array(7) {
["order_id"] => string(3) "117"
["job_id"] => string(2) "51"
["job_name"] => string(24) "商标注册"
["client_id"] => string(3) "108"
["user_name"] => string(9) "小明"
["users_id"] => string(1) "5"
["num"] => int(1)

}
[2] => array(7) {

["order_id"] => string(3) "118"
["job_id"] => string(1) "2"
["job_name"] => string(18) "外资公司注册"
["client_id"] => string(3) "103"
["user_name"] => string(10) "小红 "
["users_id"] => string(1) "2"
["num"] => int(1)

}
[3] => array(7) {

["order_id"] => string(3) "119"
["job_id"] => string(1) "2"
["job_name"] => string(18) "外资公司注册"
["client_id"] => string(3) "109"
["user_name"] => string(10) "小红 "
["users_id"] => string(1) "2"
["num"] => int(1)

}

我需要将job_name跟user_name相同的合并 并将NUM相加变成
[1] => array(7) {

["order_id"] => string(3) "117"
["job_id"] => string(2) "51"
["job_name"] => string(24) "商标注册"
["client_id"] => string(3) "108"
["user_name"] => string(9) "小明"
["users_id"] => string(1) "5"
["num"] => int(1)

}
[2] => array(7) {

["order_id"] => string(3) "118"
["job_id"] => string(1) "2"
["job_name"] => string(18) "外资公司注册"
["client_id"] => string(3) "103"
["user_name"] => string(10) "小红 "
["users_id"] => string(1) "2"
["num"] => int(2)

}

求大神们指导指导 感激不尽

回复内容:

我有这样一个数组
[1] => array(7) {

["order_id"] => string(3) "117"
["job_id"] => string(2) "51"
["job_name"] => string(24) "商标注册"
["client_id"] => string(3) "108"
["user_name"] => string(9) "小明"
["users_id"] => string(1) "5"
["num"] => int(1)

}
[2] => array(7) {

["order_id"] => string(3) "118"
["job_id"] => string(1) "2"
["job_name"] => string(18) "外资公司注册"
["client_id"] => string(3) "103"
["user_name"] => string(10) "小红 "
["users_id"] => string(1) "2"
["num"] => int(1)

}
[3] => array(7) {

["order_id"] => string(3) "119"
["job_id"] => string(1) "2"
["job_name"] => string(18) "外资公司注册"
["client_id"] => string(3) "109"
["user_name"] => string(10) "小红 "
["users_id"] => string(1) "2"
["num"] => int(1)

}

我需要将job_name跟user_name相同的合并 并将NUM相加变成
[1] => array(7) {

["order_id"] => string(3) "117"
["job_id"] => string(2) "51"
["job_name"] => string(24) "商标注册"
["client_id"] => string(3) "108"
["user_name"] => string(9) "小明"
["users_id"] => string(1) "5"
["num"] => int(1)

}
[2] => array(7) {

["order_id"] => string(3) "118"
["job_id"] => string(1) "2"
["job_name"] => string(18) "外资公司注册"
["client_id"] => string(3) "103"
["user_name"] => string(10) "小红 "
["users_id"] => string(1) "2"
["num"] => int(2)

}

求大神们指导指导 感激不尽

你这个算法简单,你自己可以写出来的!

参考如下,你自己在用着改改

$newArray = [];
foreach($array as $key=>$val){
    foreach($array as $k=>$v){
        if($val['job_name'] == $v['job_name'] && $val['user_name'] == $v['user_name']){
            $newArray = $val;
            $newArray['num'] = $val['num']+$v['num'];
        }
    }
}
相关标签: php