如何合理化PHP中的多维数组?

发布于 2024-12-04 08:14:56 字数 1788 浏览 0 评论 0原文

我一整天都在尝试这个! 我如何将顶部多维数组转换为底部。

Array (
[0] => Array ( [id] => 34 [email] => [email protected] ) 
[1] => Array ( [id] => 34 [email] => [email protected] ) 
[2] => Array ( [id] => 33 [email] => [email protected] ) 
[3] => Array ( [id] => 33 [email] => [email protected] ) 
[4] => Array ( [id] => 33 [email] => [email protected] ) 
) 

Array (
[0]=>Array ([id] => 34 [email] => Array ([0]=> [email protected] [1]=>[email protected] )
[1]=>Array ([id] => 33 [email] => Array ([0]=> [email protected] [1]=>[email protected] [2]=>[email protected])
)

非常感谢。

I've been trying this all day!
How would I convert the top multidimensional array into the bottom.

Array (
[0] => Array ( [id] => 34 [email] => [email protected] ) 
[1] => Array ( [id] => 34 [email] => [email protected] ) 
[2] => Array ( [id] => 33 [email] => [email protected] ) 
[3] => Array ( [id] => 33 [email] => [email protected] ) 
[4] => Array ( [id] => 33 [email] => [email protected] ) 
) 

Array (
[0]=>Array ([id] => 34 [email] => Array ([0]=> [email protected] [1]=>[email protected] )
[1]=>Array ([id] => 33 [email] => Array ([0]=> [email protected] [1]=>[email protected] [2]=>[email protected])
)

Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贩梦商人 2024-12-11 08:14:56
$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 等...键。

$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;
}

The first loop produces an array keyed off the id fields, and simply pushes each email address onto it. The second loop then takes that intermediate array and wraps another array around it for the 0,1,etc... keys.

箹锭⒈辈孓 2024-12-11 08:14:56

仅仅使用密钥来存储 ID 不是更简单的方法吗?像这样:

Array (
[34]=>Array ([email] => Array ([0]=> [email protected] [1]=>[email protected] )
[33]=>Array ([email] => Array ([0]=> [email protected] [1]=>[email protected] [2]=>[email protected])
)

那么对电子邮件进行分组将成为一项微不足道的任务。

Would not just using keys in order to store IDs be an easier way to do that? Like this:

Array (
[34]=>Array ([email] => Array ([0]=> [email protected] [1]=>[email protected] )
[33]=>Array ([email] => Array ([0]=> [email protected] [1]=>[email protected] [2]=>[email protected])
)

Then grouping emails would become a trivial task.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文