Rumah > pembangunan bahagian belakang > tutorial php > PHP数组合并重组问题

PHP数组合并重组问题

WBOY
Lepaskan: 2016-08-23 09:17:45
asal
785 orang telah melayarinya

我有这样一个数组
[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>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

}
[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>
Salin selepas log masuk
Salin selepas log masuk

}
[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>
Salin selepas log masuk
Salin selepas log masuk

}

我需要将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>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

}
[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>
Salin selepas log masuk
Salin selepas log masuk

}

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

回复内容:

我有这样一个数组
[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>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

}
[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>
Salin selepas log masuk
Salin selepas log masuk

}
[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>
Salin selepas log masuk
Salin selepas log masuk

}

我需要将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>
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk
Salin selepas log masuk

}
[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>
Salin selepas log masuk
Salin selepas log masuk

}

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

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

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

<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>
Salin selepas log masuk
Label berkaitan:
php
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan