search

Home  >  Q&A  >  body text

PHP数组合并重组问题

我有这样一个数组
[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)

}

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

高洛峰高洛峰2785 days ago542

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-11 10:04:04

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

    $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'];
            }
        }
    }

    reply
    0
  • 黄舟

    黄舟2017-04-11 10:04:04

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

    reply
    0
  • Cancelreply