php根據二維陣列中的某一元素相等,另乙個元素相加

2022-06-20 07:42:18 字數 2434 閱讀 3018

二維陣列:$taskdata

array

( [0] => array

( [area] => 1[winsflag] => 7[

count] => 3)

[1] => array

( [area] => 1[winsflag] => 3[

count] => 2)

[2] => array

( [area] => 1[winsflag] => 5[

count] => 2)

[3] => array

( [area] => 2[winsflag] => 6[

count] => 2)

[4] => array

( [area] => 2[winsflag] => 3[

count] => 2)

[5] => array

( [area] => 3[winsflag] => 7[

count] => 3)

[6] => array

( [area] => 3[winsflag] => 1[

count] => 1)

[7] => array

( [area] => 4[winsflag] => 2[

count] => 1)

[8] => array

( [area] => 4[winsflag] => 2[

count] => 1)

)

根據area相同將count相加

$item=;

foreach($taskdata

as$k=>$v

)else

}

結果:得到乙個新的陣列 $item

array

( [1] => array

( [area] => 1[winsflag] => 7[

count] => 7)

[2] => array

( [area] => 2[winsflag] => 6[

count] => 4)

[3] => array

( [area] => 3[winsflag] => 7[

count] => 4)

[4] => array

( [area] => 4[winsflag] => 2[

count] => 2)

)

根據需求改變一下,把相同的值取出來,其餘拼接一起

$item=;

foreach($equityinfos as $k=>$v)else

}

上面說的是根據乙個元素相等,假如要根據兩個元素相等呢,下面就來解決

array

( [0] => array

( [send_detail_id] => 65379[amount] => 1[sn] => mb12-69-181),[1] => array

( [send_detail_id] => 65380[amount] => 2[sn] =>mb12-69-181),[2] => array

( [send_detail_id] =>65380[amount] => 1[sn] => mb12-69-181),[3] => array

( [send_detail_id] => 65382[amount] => 1[sn] => sa12-64-180n2001

))如果兩個欄位send_detail_id和sn相同才合併求和,結果應該是這種

array

( [0] => array

( [send_detail_id] => 65379[amount] => 1[sn] => mb12-69-181),[1] => array

( [send_detail_id] => 65380[amount] => 3 //

和後面的相加了,第乙個就不能和他相加

[sn] =>mb12-69-181),[3] => array

( [send_detail_id] => 65382[amount] => 1[sn] => sa12-64-180n2001

))其實很簡單

foreach($arr

as$v

)else

}print_r(array_values($ret));

獲取陣列中某一元素

1 array shift 彈出陣列第乙個元素 舉例 a array new hello cool new array shift a new是第乙個元素值,取最後乙個元素可以用array pop 這種操作會改變原陣列的內容 2 array slice 擷取陣列 舉例 a array new hel...

根據list某一元素對其進行分組

今天有個小需求,簡單記錄一下,將返回的list根據其中的時間字段進行分組 describe 根據集合某一屬性建立分組返回map param param list list return author 何章懷曉 hezhanghuaixiao date 2020 9 15 17 33 other pr...

怎樣讓Python根據物件的某一元素進行排序?

以前我們對一系列元素排序以公升序或者降序時,可以建立乙個列表,然後使用自帶的sort函式解決公升序倒序問題,那麼現在,如果要物件的某個元素值進行排序?該怎樣獲取它的屬性然後對屬性進行排序呢?其實方法很簡單。python自帶的sort函式有乙個key值可以讓他按照key的指定方法去排序,譬如 sort...