Php 需要数组键来匹配另一个数组键
使用我的表格设置(我知道这不是正确的设置),如果Php 需要数组键来匹配另一个数组键,php,mysql,arrays,Php,Mysql,Arrays,使用我的表格设置(我知道这不是正确的设置),如果分支.id位于用户.notify字段中,将分包数据分组到一封电子邮件中发送给用户的最佳方式是什么 我的当前代码如下,但我无法获得匹配的分支名称(if($user\u value->branch===$key)),我只是觉得有更好的方法来完成我需要做的事情 MySQL(表的设置不是最好的,但目前它是最好的) PHP $users结果(分支仅列出一个分支,即使notify有多个分支,也可以) $branch_用户结果 Array ( [0] =
分支.id
位于用户.notify
字段中,将分包
数据分组到一封电子邮件中发送给用户的最佳方式是什么
我的当前代码如下,但我无法获得匹配的分支名称(if($user\u value->branch===$key)
),我只是觉得有更好的方法来完成我需要做的事情
MySQL(表的设置不是最好的,但目前它是最好的)
PHP
$users结果(分支仅列出一个分支,即使notify有多个分支,也可以)
$branch_用户结果
Array
(
[0] => Array
(
[email] => user_1@example.com
[body] => The following news was posted on Branch 1 at example.com<br />Click the title to read more.<br /><ul><li><date><br /><a href="http://example.com/branch_1/?post=147">Subcontent 1</a><br /></li></ul><br />The following news was posted on Branch 1 at example.com<br />Click the title to read more.<br /><ul><li><date><br /><a href="http://example.com/branch_1/?post=588">Subcontent 2</a><br /></li></ul><br />The following news was posted on Branch 1 at example.com<br />Click the title to read more.<br /><ul><li><date><br /><a href="http://example.com/branch_1/?post=696">Subcontent 3</a><br /></li></ul><br />
)
[1] => Array
(
[email] => user_2@example.com
[body] => The following news was posted on Branch 2 at example.com<br />Click the title to read more.<br /><ul><li><date><br /><a href="http://example.com/branch_2/?post=696">Subcontent 3</a><br /></li></ul><br />The following news was posted on Branch 3 at example.com<br />Click the title to read more.<br /><ul><li><date><br /><a href="http://example.com/branch_3/?post=588">Subcontent 2</a><br /></li></ul><br />
)
[2] => Array
(
[email] => user_3@example.com
[body] => The following news was posted on Branch 3 at example.com<br />Click the title to read more.<br /><ul><li><date><br /><a href="http://example.com/branch_3/?post=147">Subcontent 1</a><br /></li></ul><br />
)
)
数组
(
[0]=>阵列
(
[电子邮件]=>用户_1@example.com
[正文]=>以下新闻发布在example.com的分支1上
点击标题阅读更多。
以下新闻发布在example.com的分支1上
点击标题阅读更多。
)
[1] =>阵列
(
[电子邮件]=>用户_2@example.com
[body]=>以下新闻发布在example.com的分支2
点击标题阅读更多。
以下新闻发布在example.com的分支3
点击标题阅读更多。
阵列
(
[电子邮件]=>用户_3@example.com
[body]=>以下新闻已发布在example.com的分支3上
点击标题阅读更多信息。
)
)
我不明白你的问题是什么。您已经知道您的表设置不正确。那么你到底期望什么呢?我试图集中精力去理解你的需求,但这并不是那么容易……如果你说出你想要实现的目标,也许会更容易。即使是您正在寻找的$user\u value->branch所需的输出也有如下名称(branch 1,branch 3)。您的密钥是(分支1、分支3)。所以它们不匹配。@phaberest我已经在底部添加了所需的结果。我知道这让人困惑,但修理桌子不是一个选项:(@MartavisGriffin那只是我帖子中的一个错误。我不明白你的问题是什么。你已经知道你的表格设置不正确。那么你到底期望什么呢?我正试图集中精力了解你的需求,但这并不是那么容易……如果你说出你想要实现的目标,也许会更容易。即使只是想要的输出t您正在查找$user\u value->branch的名称类似于(branch 1,branch 3)。您的键是(branch\u 1,branch\u 3)。因此它们将不匹配。@phaberest我已将所需结果添加到底部。我知道这很混乱,但修复表不是一个选项:(@MartavisGriffin这只是我帖子中的一个错误
# Get current date.
$datetime_obj=new DateTime();
# Format the current date.
$today=$datetime_obj->format('Y-m-d');
# Subtract 7 days from the current date.
$datetime_obj->sub(new DateInterval('P7D'));
# Format the past date.
$one_week_ago=$datetime_obj->format('Y-m-d');
$sql="SELECT `sc`.`id`,
`sc`.`title`,
`sc`.`date`,
`b`.`branch`
FROM `subcontent` AS sc
INNER JOIN `branches` AS b ON SUBSTRING_INDEX(TRIM(BOTH '-' FROM `sc`.`branch`), '-', 1)=`b`.`id`
WHERE `sc`.`date` >= $one_week_ago
ORDER BY `sc`.`date` DESC";
$all_subcontent=$db->get_results($sql);
# If there was content posted in the last week...
if(!empty($all_subcontent))
{
$branch_content=array();
# Loop through the subcontent.
foreach($all_subcontent as $subcontent)
{
# Build a new array.
$branch_content[$subcontent->branch][]=array(
'id'=>$subcontent->id,
'title'=>$subcontent->title,
'date'=>$subcontent->date,
'domain'=>$subcontent->domain
);
}
# Results below.
//print_r($branch_content);
$users_sql="SELECT `u`.`email`,
`u`.`notify`,
`b`.`branch`
FROM `users` AS u
INNER JOIN `branches` AS b ON SUBSTRING_INDEX(TRIM(BOTH '-' FROM `u`.`notify`), '-', 1)=`b`.`id`
WHERE `u`.`notify` IS NOT NULL";
$users=$db->get_results($users_sql);
# Results below.
//print_r($users);
$branch_users=array();
$subject=DOMAIN_NAME.' News: '.$one_week_ago.' to '.$today;
# Loop through the users.
foreach($users as $user_key=>$user_value)
{
# Loop through the new array.
foreach($branch_content as $key=>$value)
{
# Branch names never match up!
# Does the users branch match the branch we're currently looping through?
if($user_value->branch===$key)
{
# Build a new array.
$branch_users[$user_key]['email']=$user_value->email;
$branch_users[$user_key]['body']='The following news was posted on '.$key.' at '.DOMAIN_NAME.'<br />'.
'Click the title to read more.<br />'.
'<ul>';
foreach($value as $row)
{
$branch_users[$user_key]['body'].='<li>'.
$row['date'].'<br />'.
'<a href="http://'.DOMAIN_NAME.'/'.$key.'/?post='.$row['id'].'">'.$row['title'].'</a><br />'.
'</li>';
}
$branch_users[$user_key]['body'].='</ul><br />';
}
}
}
# Results below.
//print_r($branch_users);
# Loop through $branch_users and send emails
}
Array
(
[branch_1] => Array
(
[0] => Array
(
[id] => 147
[title] => Subcontent 1
)
[1] => Array
(
[id] => 588
[title] => Subcontent 2
)
[2] => Array
(
[id] => 696
[title] => Subcontent 3
)
)
[branch_2] => Array
(
[0] => Array
(
[id] => 696
[title] => Subcontent 3
)
)
[branch_3] => Array
(
[0] => Array
(
[id] => 147
[title] => Subcontent 1
)
)
)
Array
(
[0] => stdClass Object
(
[email] => user_1@example.com
[notify] => -50-
[branch] => branch_1
)
[1] => stdClass Object
(
[email] => user_2@example.com
[notify] => -70-100-
[branch] => branch_2
)
[2] => stdClass Object
(
[email] => user_3@example.com
[notify] => -100-
[branch] => branch_3
)
)
Array
(
[0] => Array
(
[email] => user_1@example.com
[body] => The following news was posted on Branch 1 at example.com<br />Click the title to read more.<br /><ul><li><date><br /><a href="http://example.com/branch_1/?post=147">Subcontent 1</a><br /></li></ul><br />The following news was posted on Branch 1 at example.com<br />Click the title to read more.<br /><ul><li><date><br /><a href="http://example.com/branch_1/?post=588">Subcontent 2</a><br /></li></ul><br />The following news was posted on Branch 1 at example.com<br />Click the title to read more.<br /><ul><li><date><br /><a href="http://example.com/branch_1/?post=696">Subcontent 3</a><br /></li></ul><br />
)
[1] => Array
(
[email] => user_2@example.com
[body] => The following news was posted on Branch 2 at example.com<br />Click the title to read more.<br /><ul><li><date><br /><a href="http://example.com/branch_2/?post=696">Subcontent 3</a><br /></li></ul><br />The following news was posted on Branch 3 at example.com<br />Click the title to read more.<br /><ul><li><date><br /><a href="http://example.com/branch_3/?post=588">Subcontent 2</a><br /></li></ul><br />
)
[2] => Array
(
[email] => user_3@example.com
[body] => The following news was posted on Branch 3 at example.com<br />Click the title to read more.<br /><ul><li><date><br /><a href="http://example.com/branch_3/?post=147">Subcontent 1</a><br /></li></ul><br />
)
)