我已经试了一整天了!如何将顶部多维数组转换为底部多维数组
Array (
[0] => Array ( [id] => 34 [email] => a@example.com )
[1] => Array ( [id] => 34 [email] => b@example.com )
[2] => Array ( [id] => 33 [email] => c@example.com )
[3] => Array ( [id] => 33 [email] => d@example.com )
[4] => Array ( [id] => 33 [email] => e@example.com )
)
Array (
[0]=>Array ([id] => 34 [email] => Array ([0]=> a@example.com [1]=>b@example.com )
[1]=>Array ([id] => 33 [email] => Array ([0]=> c@example.com [1]=>d@example.com [2]=>e@example.com)
)
许多谢谢。$new_array = array();
foreach ($orig_array as $child) {
$new_array[$child['id']][] = $child['email'];
}
$final_array = array();
foreach($new_array as $child) {
$final_array[] = $child;
}
第一个循环生成一个以id字段为键的数组,并简单地将每个电子邮件地址推入其中。然后,第二个循环使用中间数组,并在它周围包装另一个数组,用于0,1等…钥匙。
仅仅使用键来存储id不是一种更简单的方法吗?这样的:
Array (
[34]=>Array ([email] => Array ([0]=> a@example.com [1]=>b@example.com )
[33]=>Array ([email] => Array ([0]=> c@example.com [1]=>d@example.com [2]=>e@example.com)
)