首页  >  文章  >  后端开发  >  PHP数组合并重组问题

PHP数组合并重组问题

WBOY
WBOY原创
2016-08-23 09:17:45751浏览

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

<code>["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)</code>

}
[2] => array(7) {

<code>["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)</code>

}
[3] => array(7) {

<code>["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)</code>

}

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

<code>["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)</code>

}
[2] => array(7) {

<code>["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)</code>

}

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

回复内容:

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

<code>["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)</code>

}
[2] => array(7) {

<code>["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)</code>

}
[3] => array(7) {

<code>["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)</code>

}

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

<code>["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)</code>

}
[2] => array(7) {

<code>["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)</code>

}

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

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

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

<code>$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'];
        }
    }
}</code>
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn