PHP数组合并重组问题
程序员文章站
2022-06-05 16:21:57
...
我有这样一个数组
[1] => array(7) {
[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实现 使用PHP数组内部指针操作函数
-
php下将多个数组合并成一个数组的方法与实例代码
-
PHP 多维数组的排序问题 根据二维数组中某个项排序
-
php通过array_merge()函数合并两个数组的方法
-
php通过array_merge()函数合并关联和非关联数组的方法
-
基于php实现随机合并数组并排序(原排序)
-
php 数组的合并、拆分、区别取值函数集
-
PHP数组的交集array_intersect(),array_intersect_assoc(),array_inter_key()函数的小问题
-
PHP中通过加号合并数组的一个简单方法分享
-
php中二维数组排序问题方法详解