php 数组 - 为每个唯一的电子邮件地址发送一封电子邮件,将数据分组在一起
我的数组中有无限数量的项目:
$query = mysql_query("SELECT * FROM orders WHERE orderID = '$orderID'");
while($row = mysql_fetch_array($query)){
$items[] = $row[itemnumber];
$quantity[] = $row[quantity];
$vendoremail[] = $row[vendoremail];
}
数组中的值可能是这样的:
$items = ("A","B", "C");
$quantity = ("2", "1", "3");
$vendoremail = ("[email protected]", "[email protected]", "[email protected]");
我想对来自相同电子邮件地址的数据进行分组,并向每个地址发送一封电子邮件。
因此,一封电子邮件将发送至 [email protected],其中包含项目“A”电子邮件正文中的“C”以及数量“2”和“3”。
另一封电子邮件将发送至 [email protected],其中包含项目“B”以及电子邮件正文中的数量“1”。
有人知道实现此目标的最佳方法吗?
I have an indefinite amount of items in an array:
$query = mysql_query("SELECT * FROM orders WHERE orderID = '$orderID'");
while($row = mysql_fetch_array($query)){
$items[] = $row[itemnumber];
$quantity[] = $row[quantity];
$vendoremail[] = $row[vendoremail];
}
The values in the arrays could be something like this:
$items = ("A","B", "C");
$quantity = ("2", "1", "3");
$vendoremail = ("[email protected]", "[email protected]", "[email protected]");
I want to group the data from the same email addresses and send a single email to each address.
So one email gets sent to [email protected] with the items "A" and "C" and quantity "2" and "3" in the body of the email.
And another email gets sent to [email protected] with the items "B" and quantity "1" in the body of the email.
Anybody know the best way to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过 GROUP_CONCAT() 将这些值分组为逗号分隔的字符串,并可以选择在 PHP 中将它们拆分回来:
这将产生如下结果:
在 PHP 中,如果需要,您可以
explode( )
列表返回到数组中以显示在电子邮件正文中。You can group the values into comma-separated strings via
GROUP_CONCAT()
and optionally split them back up in PHP:This will produce results like:
In PHP, if you need to you can
explode()
the lists back out into arrays to display in the email body.您可以使用电子邮件作为键创建两个数组($items 和 $quantity):
然后,只需迭代它们,检索每个数组包含的所有数据:
You could create the two arrays ($items and $quantity) using the e-mail as the key:
Then, just iterate over them, retrieving all the data each one contains:
只是让我感到畏缩,但是将你的键名用引号括在数组中!
在你原来的 while 循环中你可以做类似的事情。
这只是我写的一个快速的东西,所以我只是不小心在其中添加了 \n 换行符,但是从上面的代码中您应该希望使用多维数组来了解要点。
编辑:为了获得奖励积分,就样式和数据库设计而言,您确实应该有一个包含供应商 ID、供应商名称、电子邮件、联系信息等的供应商表然后在订单表上有一个指向供应商 ID 的外键列,这样您就可以加入它,并且不会在每行中存储潜在的重复和易失性(电子邮件地址可以随时更改)数据。只是我的题外优化 0.02 美元。
Just making me cringe, but wrap your key names in arrays with quotes!
in your original while loop you could do something like.
This was just a quick thing I wrote up, so I just carelessly threw \n linebreaks in there, but the from above code you should hopefully get the gist using multi-dimensional arrays.
Edit: For bonus points, as a matter of style and database design, you really should have a vendor table with a vendor ID, vendor name, email, contact info, etc and then have a foreign key column on the order table that points to a vendor ID, so you can JOIN to it and not store potentially duplicate and volatile (email addrs can change at any moment) data in each row. Just my off-topic optimization $0.02.
鉴于 array-n 的每个成员映射到 array-n+1 我将执行以下操作:
现在您可以迭代 groups 数组,其中每个电子邮件键都有一个或多个数组
Given the fact that each member of array-n maps to array-n+1 I would do the following:
and now you can iterate through the groups array which each email key having one or more arrays