當前位置:首頁 » 編程語言 » php合並二維數組

php合並二維數組

發布時間: 2025-03-30 04:43:18

1. php合並兩個二維數組,如果兩個二維數組的id值相等,則合並為一個新的數組

id本應該是唯一性的鍵值,利用好就行。以下輸出沒有對id排序,因為覺得沒必要。
需要時可以再排。
function mergeById(&$a,&$b){
$c=array();
foreach($a as $e) $c[$e['id']]=$e;
foreach($b as $e) $c[$e['id']]=isset($c[$e['id']])? $c[$e['id']]+$e : $e;
return $c;
}
$a=//數組一;
$b=//數組二;
var_mp(mergeById($b,$a));
===========
array
1 =>
array
'id' => int 1
'name' => string 'test' (length=4)
'subject' => string 'subject' (length=7)
2 =>
array
'id' => int 2
'name' => string 'test2' (length=5)
3 =>
array
'id' => int 3
'name' => string 'test3' (length=5)
'subject' => string 'subject3' (length=8)

2. php 如何把多個一維數組合並成二維數組

你可以這樣操作,新建一個數組,然後用array_push()函數,依次把2個一維數據進行壓棧處理。然後就成了一個二維數組了。

熱點內容
nuttx編譯 發布:2025-04-01 14:26:20 瀏覽:683
看視頻軟體蘋果和安卓哪個好 發布:2025-04-01 14:21:55 瀏覽:631
我的世界伺服器地址用公網ip可以嗎 發布:2025-04-01 14:18:22 瀏覽:100
電腦外接網路伺服器 發布:2025-04-01 14:17:08 瀏覽:694
一體機玩魔獸世界哪個配置好 發布:2025-04-01 13:47:20 瀏覽:502
c異或加密 發布:2025-04-01 13:40:20 瀏覽:243
c語言編譯器推薦mac版 發布:2025-04-01 13:35:08 瀏覽:345
matlab與資料庫連接 發布:2025-04-01 13:30:01 瀏覽:397
資料庫平均值 發布:2025-04-01 13:28:00 瀏覽:532
安卓數據緩存在哪裡 發布:2025-04-01 13:22:13 瀏覽:367