正如標題的價值觀說我得到陣列看起來像這樣:合併嵌套數組
array (size=376)
0 =>
array (size=3)
'source' => int 0
'target' => int 47
'officers' =>
array (size=1)
0 => string 'PARKER, Thomas, Sir' (length=19)
1 =>
array (size=3)
'source' => int 0
'target' => int 104
'officers' =>
array (size=1)
0 => string 'STEVENS, Anne' (length=13)
2 =>
array (size=3)
'source' => int 0
'target' => int 187
'officers' =>
array (size=1)
0 => string 'PARKER, Thomas, Sir' (length=19)
3 =>
array (size=3)
'source' => int 0
'target' => int 229
'officers' =>
array (size=1)
0 => string 'GROTE, Byron' (length=12)
4 =>
array (size=3)
'source' => int 0
'target' => int 244
'officers' =>
array (size=1)
0 => string 'GROTE, Byron' (length=12)
5 =>
array (size=3)
'source' => int 1
'target' => int 60
'officers' =>
array (size=1)
0 => string 'BASON, John' (length=11)
6 =>
array (size=3)
'source' => int 1
'target' => int 92
'officers' =>
array (size=1)
0 => string 'HAUSER, Wolfhart, Dr' (length=20)
如果源和目標都是一樣的我想這個陣列中創建1個值。例如
Insteed這個:
5 =>
array (size=3)
'source' => int 1
'target' => int 92
'officers' =>
array (size=1)
0 => string 'BASON, John' (length=11)
6 =>
array (size=3)
'source' => int 1
'target' => int 92
'officers' =>
array (size=1)
0 => string 'HAUSER, Wolfhart, Dr' (length=20)
我會想
5 =>
array (size=3)
'source' => int 1
'target' => int 92
'officers' =>
array (size=1)
0 => string 'BASON, John' (length=11)
1 => string 'HAUSER, Wolfhart, Dr' (length=20)
我的想法來實現這一點:
for ($i = 0; $i < count($edges); $i++) {
for ($j = $i + 1; $j < count($edges); $j++) {
if($edges[$i]['source']==$edges[$j]['source']&&$edges[$i]['target']==$edges[$j]['target']){
foreach($edges[$j]['officers'] as $officer){
array_push($edges[$i]['officers'], $officer);
}
array_splice($edges, $j,1);
}
}
}
這似乎是合併,但只有2個值,它不刪除舊值,以便:
我有一樣的東西:
66 =>
array (size=3)
'source' => int 24
'target' => int 103
'officers' =>
array (size=2)
0 => string 'GILAURI, Irakli' (length=15)
1 => string 'JANIN, Neil' (length=11)
67 =>
array (size=3)
'source' => int 24
'target' => int 103
'officers' =>
array (size=1)
0 => string 'MORRISON, David' (length=15)
所以它合併了Gilauri和Janin,但沒有將Morrison,David添加到官員陣列,還添加了Gilauri,Irakli和Janin,Neil在這個數組中添加了不應該添加的單獨行。
這真的是對OP代碼問題的一個很好的解釋。 –
@bwoebi謝謝你,像魅力一樣工作。我很高興我至少在某種程度上接近它的工作。 – Higeath