I'm trying to join two associative arrays together based on an entry_id
key. Both arrays come from individual database resources, the first stores entry titles, the second stores entry authors, the key=>value pairs are as follows:
我正在尝试基于entry_id键将两个关联数组连接在一起。两个数组都来自单个数据库资源,第一个存储条目标题,第二个存储条目作者,key => value对如下:
array (
'entry_id' => 1,
'title' => 'Test Entry'
)
array (
'entry_id' => 1,
'author_id' => 2
I'm trying to achieve an array structure like:
我正在尝试实现如下的数组结构:
array (
'entry_id' => 1,
'author_id' => 2,
'title' => 'Test Entry'
)
Currently, I've solved the problem by looping through each array and formatting the array the way I want, but I think this is a bit of a memory hog.
目前,我通过循环遍历每个数组并按照我想要的方式格式化数组来解决问题,但我认为这有点像记忆。
$entriesArray = array();
foreach ($entryNames as $names) {
foreach ($entryAuthors as $authors) {
if ($names['entry_id'] === $authors['entry_id']) {
$entriesArray[] = array(
'id' => $names['entry_id'],
'title' => $names['title'],
'author_id' => $authors['author_id']
);
}
}
}
I'd like to know is there an easier, less memory intensive method of doing this?
我想知道有一种更简单,内存更少的方法吗?
4 个解决方案
#1
2
Is it possible you can do a JOIN in the SQL used to retrieve the information from the database rather than fetching the data in multiple queries? It would be much faster and neater to do it at the database level.
您是否可以在用于从数据库中检索信息的SQL中执行JOIN,而不是在多个查询中获取数据?在数据库级别执行它会更快更整洁。
Depending on your database structure you may want to use something similar to
根据您的数据库结构,您可能希望使用类似的东西
SELECT entry_id, title, author_id
FROM exp_weblog_data
INNER JOIN exp_weblog_titles
ON exp_weblog_data.entry_id = exp_weblog_titles.entry_id
WHERE field_id_53 = "%s" AND WHERE entry_id IN ("%s")
Wikipedia has a bit on each type of join
*对每种类型的连接都有一点看法
Otherwise the best option may be to restructure the first array so that it is a map of the entry_id to the title
否则,最佳选择可能是重构第一个数组,使其成为title_id到标题的映射
So:
所以:
array(
array(
'entry_id' => 1,
'title' => 'Test Entry 1',
),
array(
'entry_id' => 3,
'title' => 'Test Entry 2',
),
)
Would become:
会成为:
array(
1 => 'Test Entry 1',
3 => 'Test Entry 2',
)
Which would mean the code required to merge the arrays is simplified to this:
这意味着合并数组所需的代码简化为:
$entriesArray = array();
foreach ($entryAuthors as $authors) {
$entriesArray[] = array(
'id' => $authors['entry_id'],
'title' => $entryNames[$authors['entry_id']],
'author_id' => $authors['author_id']
);
}
#2
1
I've rearranged some of my code to allow for a single SQL query, which looks like:
我重新安排了一些代码以允许单个SQL查询,如下所示:
$sql = sprintf('SELECT DISTINCT wd.field_id_5, wd.entry_id, mb.email, mb.screen_name
FROM `exp_weblog_data` wd
INNER JOIN `exp_weblog_titles` wt
ON wt.entry_id=wd.entry_id
INNER JOIN `exp_members` mb
ON mb.member_id=wt.author_id
WHERE mb.member_id IN ("%s")
AND wd.entry_id IN ("%s")',
join('","', array_unique($authors)),
join('","', array_unique($ids))
);
This solves my problem quite nicely, even though I'm making another SQL call. Thanks for trying.
这很好地解决了我的问题,即使我正在进行另一个SQL调用。谢谢你的尝试。
#3
0
In response to your comment on Yacoby's post, will this SQL not give the output you are after?
在回应你对Yacoby帖子的评论时,这个SQL会不会给你输出的输出?
SELECT exp_weblog_data.entry_id, exp_weblog_data.field_id_5 AS title_ie, exp_weblog_titles.author_id
FROM exp_weblog_data LEFT JOIN exp_weblog_titles
ON exp_weblog_data.entry_id = exp_weblog_titles.entry_id
WHERE exp_weblog_data.field_id_53 = "%S"
Every entry in exp_weblog_data where field_id_53 = "%S" will be joined with any matching authors in exp_weblog_titles, if a an entry has more than one author, two or more rows will be returned.
exp_weblog_data中的每个条目,其中field_id_53 =“%S”将与exp_weblog_titles中的任何匹配作者连接,如果条目具有多个作者,则将返回两行或更多行。
#4
-1
see http://php.net/manual/en/function.array-merge.php
见http://php.net/manual/en/function.array-merge.php
#1
2
Is it possible you can do a JOIN in the SQL used to retrieve the information from the database rather than fetching the data in multiple queries? It would be much faster and neater to do it at the database level.
您是否可以在用于从数据库中检索信息的SQL中执行JOIN,而不是在多个查询中获取数据?在数据库级别执行它会更快更整洁。
Depending on your database structure you may want to use something similar to
根据您的数据库结构,您可能希望使用类似的东西
SELECT entry_id, title, author_id
FROM exp_weblog_data
INNER JOIN exp_weblog_titles
ON exp_weblog_data.entry_id = exp_weblog_titles.entry_id
WHERE field_id_53 = "%s" AND WHERE entry_id IN ("%s")
Wikipedia has a bit on each type of join
*对每种类型的连接都有一点看法
Otherwise the best option may be to restructure the first array so that it is a map of the entry_id to the title
否则,最佳选择可能是重构第一个数组,使其成为title_id到标题的映射
So:
所以:
array(
array(
'entry_id' => 1,
'title' => 'Test Entry 1',
),
array(
'entry_id' => 3,
'title' => 'Test Entry 2',
),
)
Would become:
会成为:
array(
1 => 'Test Entry 1',
3 => 'Test Entry 2',
)
Which would mean the code required to merge the arrays is simplified to this:
这意味着合并数组所需的代码简化为:
$entriesArray = array();
foreach ($entryAuthors as $authors) {
$entriesArray[] = array(
'id' => $authors['entry_id'],
'title' => $entryNames[$authors['entry_id']],
'author_id' => $authors['author_id']
);
}
#2
1
I've rearranged some of my code to allow for a single SQL query, which looks like:
我重新安排了一些代码以允许单个SQL查询,如下所示:
$sql = sprintf('SELECT DISTINCT wd.field_id_5, wd.entry_id, mb.email, mb.screen_name
FROM `exp_weblog_data` wd
INNER JOIN `exp_weblog_titles` wt
ON wt.entry_id=wd.entry_id
INNER JOIN `exp_members` mb
ON mb.member_id=wt.author_id
WHERE mb.member_id IN ("%s")
AND wd.entry_id IN ("%s")',
join('","', array_unique($authors)),
join('","', array_unique($ids))
);
This solves my problem quite nicely, even though I'm making another SQL call. Thanks for trying.
这很好地解决了我的问题,即使我正在进行另一个SQL调用。谢谢你的尝试。
#3
0
In response to your comment on Yacoby's post, will this SQL not give the output you are after?
在回应你对Yacoby帖子的评论时,这个SQL会不会给你输出的输出?
SELECT exp_weblog_data.entry_id, exp_weblog_data.field_id_5 AS title_ie, exp_weblog_titles.author_id
FROM exp_weblog_data LEFT JOIN exp_weblog_titles
ON exp_weblog_data.entry_id = exp_weblog_titles.entry_id
WHERE exp_weblog_data.field_id_53 = "%S"
Every entry in exp_weblog_data where field_id_53 = "%S" will be joined with any matching authors in exp_weblog_titles, if a an entry has more than one author, two or more rows will be returned.
exp_weblog_data中的每个条目,其中field_id_53 =“%S”将与exp_weblog_titles中的任何匹配作者连接,如果条目具有多个作者,则将返回两行或更多行。
#4
-1
see http://php.net/manual/en/function.array-merge.php
见http://php.net/manual/en/function.array-merge.php