简体   繁体   English

无法合并两个数组

[英]Trouble combining two arrays

I'll start with the arrays, to make the picture clear. 我将从数组开始,以使图片清晰。 I get both arrays from my database. 我从数据库中得到两个数组。

Here's one record from Array 1 这是数组1中的一条记录

Array ( [0] => Array ( [vraag_id] => 2 [vraag_titel] => Je bent geen randdebiel he [categorie_id] => 1 [categorie_naam] => Eierstokkanker / Ovarium [gebruiker] => Bas Koesveld [reacties] => 13 [vraag_inhoud] => Omdat ik het zeg verdomme! [vraag_datum] => 2012-11-19 00:00:00 ) )

Here's part complete record from Array 2 这是阵列2中的部分完整记录

Array ( [0] => Array ( [vraag_id] => 1 [resultaat] => 2 ) [1] => Array ( [vraag_id] => 2 [resultaat] => 1 ) [2] => Array ( [vraag_id] => 3 [resultaat] => 1 ) [3] => Array ( [vraag_id] => 4 [resultaat] => 1 ) [4] => Array ( [vraag_id] => 5 [resultaat] => 1 ) [5] => Array ( [vraag_id] => 6 [resultaat] => 1 ) [6] => Array ( [vraag_id] => 7 [resultaat] => 1 ) [7] => Array ( [vraag_id] => 8 [resultaat] => 1 ) [8] => Array ( [vraag_id] => 9 [resultaat] => 1 ) [9] => Array ( [vraag_id] => 10 [resultaat] => 1 ) [10] => Array ( [vraag_id] => 11 [resultaat] => 1 ) [11] => Array ( [vraag_id] => 12 [resultaat] => 1 ) )

I want to add the column resultaat from array 2, to the record of array 1 where vraag_id matches. 我想将数组2中的列resultaat添加到vraag_id匹配的数组1的记录中。

I'm pretty new to PHP and have been trying a lot, without success. 我对PHP很陌生,已经尝试了很多,但没有成功。 Anyone who could help me? 有人可以帮助我吗?

EDIT: 编辑:

Thanks for the comments all! 感谢您的所有评论! I think I think I should make myself a bit more clear. 我想我应该让自己更清楚一点。 Both content comes from the same table, but with different queries. 两种内容都来自同一表,但查询不同。 The queries are: 查询是:

SELECT T.id AS vraag_id, T.titel AS vraag_titel, C.id AS categorie_id, C.naam AS categorie_naam, L.gebruikersnaam AS gebruiker, P.inhoud AS vraag_inhoud, P.datum AS vraag_datum 
FROM categorie C
JOIN topic T ON C.id = T.categorie
JOIN post P ON T.id = P.vraag
JOIN lid L ON P.lid = L.id 
WHERE P.lid = 2 
ORDER BY P.datum DESC

This gives me the the desired results from the database. 这为我提供了数据库所需的结果。 Now I want to count how many replies there are for one question and I do that with this query: 现在,我要计算一个问题的回复数,并使用以下查询进行操作:

SELECT vraag AS vraag_id, COUNT( * )
FROM post
WHERE lid = 2
GROUP BY vraag_id

Now how do I join these queries? 现在如何加入这些查询?

Thanks a lot! 非常感谢!

The best thing you can do is joining the result in the query to the database. 最好的办法是将查询中的结果加入数据库。

Said that, your problem is quite simple to solve: 也就是说,您的问题很容易解决:

// rework your array2:
$temp = array();
foreach ($array2 as $index) {
    $temp[$index['vraag_id']]=$index['resultaat'];
}

// then add the resultaat to the right record:

for($i=0; $i< count($array1); $i++) {
    $array1[$i]['resultaat'] = $temp[$array1[$i]['vraag_id']];
}

By the way I suggest, another time to use the sql join to obtain your result in a more efficient and elegant way. 顺便说一句,我建议您再一次使用sql join以更有效,更优雅的方式获取结果。

Have you tried to compose the two queries like this? 您是否尝试过像这样编写两个查询?

SELECT T.id AS vraag_id, T.titel AS vraag_titel, C.id AS categorie_id, C.naam AS 
       categorie_naam, L.gebruikersnaam AS gebruiker, P.inhoud AS vraag_inhoud, 
       P.datum AS vraag_datum, res.resultaat 
FROM categorie C
JOIN topic T ON C.id = T.categorie
JOIN post P ON T.id = P.vraag
JOIN lid L ON P.lid = L.id 
JOIN (SELECT vraag AS vraag_id, COUNT( * ) as resultaat
    FROM post
    WHERE lid = 2
    GROUP BY vraag_id) as res ON T.id = res.id
WHERE P.lid = 2 
ORDER BY P.datum DESC

I've not a mysql at hand right now and can't test by myself but, excluding typos it should work). 我现在还没有mysql,无法自己进行测试,但是排除错别字应该可以使用)。

The trick is to use a subquery on the same table and join it on the fly 诀窍是在同一个表上使用子查询并快速将其联接

Assuming only one entry in the second array for each vraag_id in the first array, then roughly (and most simply): 假设第一个数组中的每个vraag_id仅在第二个数组中有一个条目,则大致(最简单):

<?php

foreach ($array2 as $a2i)
{
    foreach ($array1 as $a1i)
    {
        if ($a2i['vraag_id'] == $a1i['vraag_id'])
        {
            $a1i['result'] = $a2i['resultaat'];
        }
    }
}

There would be a trivial change required to allow multiple results per vraag_id. 为了允许每个vraag_id允许多个结果,需要进行一些细微的更改。

Alternatively do this at the database level with (probably) a LEFT JOIN. 或者,可以(可能)使用LEFT JOIN在数据库级别执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM