首頁 >後端開發 >php教程 >相同数组的组合为新数组

相同数组的组合为新数组

WBOY
WBOY原創
2016-06-06 20:38:451027瀏覽

<code>array (size=2)
  'id' => int 139
  'key' => string '11111' 

array (size=2)
  'id' => int 139
  'key' => string '22222' 

array (size=2)
  'id' => int 159
  'key' => string '33333'

array (size=2)
  'id' => int 159
  'key' => string '44444'
</code>

合并为

<code>array (size=2)
  'id' => int 139
  'key' => string '11111,22222' 

array (size=2)
  'id' => int 159
  'key' => string '33333,44444'
</code>

回复内容:

<code>array (size=2)
  'id' => int 139
  'key' => string '11111' 

array (size=2)
  'id' => int 139
  'key' => string '22222' 

array (size=2)
  'id' => int 159
  'key' => string '33333'

array (size=2)
  'id' => int 159
  'key' => string '44444'
</code>

合并为

<code>array (size=2)
  'id' => int 139
  'key' => string '11111,22222' 

array (size=2)
  'id' => int 159
  'key' => string '33333,44444'
</code>

<code>$arr = [
    ["id"=>139, "key"=>"111"],
    ["id"=>139, "key"=>"222"],
    ["id"=>159, "key"=>"333"],
    ["id"=>159, "key"=>"444"]
];

$res = [];
foreach( $arr as $item ) $res[ $item['id'] ][] = $item['key'];
</code>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn