Reputation: 466
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
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
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.
I'm pretty new to PHP and have been trying a lot, without success. 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!
Upvotes: 1
Views: 100
Reputation: 14909
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.
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).
The trick is to use a subquery on the same table and join it on the fly
Upvotes: 1
Reputation: 2180
Assuming only one entry in the second array for each vraag_id in the first array, then roughly (and most simply):
<?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.
Alternatively do this at the database level with (probably) a LEFT JOIN.
Upvotes: 0