Reputation: 837
Even when I didn't use a nested loop it produced duplicates. I finally made a dirty fix by setting the updates array key to the update key, so at least a duplicate would be overwritten... I swear it wasn't the data. The update ID is my primary key, so there's no way there's duplicates in the db, also I'm looking at my db right now and there's no duplicates.
Anyway, not a pretty solution, but I guess this solved it:
foreach( $L_a_updates as $update ){
if( array_key_exists($update['eventId'], $L_a_events) ){
$L_a_events[ $update['eventId'] ]['updates'][ $update['updateId'] ] = $update;
}
}
The answers below are good though, and clearly by more capable minds than mine at the moment. TGIF.
It's a Friday afternoon, so it's probably something extremely dumb, but I can't get this to work properly.
I've got two arrays, $L_a_events
and $L_a_updates
. Every event can have 0 or more updates. I fetch the events from the db, then I fetch the updates, and then I run them through a loop to compare event IDs and add the updates to their events. So the final structure is something like:
Array(
[0] => Array(
['eventId'] => 2,
['message'] => 'Some message',
['updates'] => Array(
[0] => Array (
['updateId'] => 123,
['eventId'] => 2,
['message'] => 'Some update message',
)
)
)
);
I run this loop to achieve that:
foreach( $L_a_events as $key => $event ){
foreach( $L_a_updates as $update ){
if($update['eventId'] == $event['eventId']){
$L_a_events[$key]['updates'][] = $update;
}
}
}
Is something wrong with my loop?! The resultset from the database shows no duplicates. When I print both arrays before I run the loop, they both look fine.
It's only after this loop that for some reason a single update is added to an event array twice. Also, it doesn't do that with every update.
So after the loop, instead of the above array, I get this:
Array(
[0] => Array(
['eventId'] => 2,
['message'] => 'Some message',
['updates'] => Array(
[0] => Array (
['updateId'] => 123,
['eventId'] => 2,
['message'] => 'Some update message',
),
[1] => Array (
['updateId'] => 123,
['eventId'] => 2,
['message'] => 'Some update message',
)
)
)
);
Upvotes: 0
Views: 134
Reputation: 1220
Your code is functional. It's not optimal in anyway as per the previous answers. Check your data.
<?php
$L_a_events = array(0=>array('eventId'=>1),1=>array('eventId'=>2));
$L_a_updates = array(0=>array('updateId'=>1,'eventId'=>1),1=>array('updateId'=>2,'eventId'=>1),2=>array('updateId'=>3,'eventId'=>2));
foreach( $L_a_events as $key => $event ){
foreach( $L_a_updates as $update ){
if($update['eventId'] == $event['eventId']){
$L_a_events[$key]['updates'][] = $update;
}
}
}
print_r($L_a_events);
?>
Output:
Array
(
[0] => Array
(
[eventId] => 1
[updates] => Array
(
[0] => Array
(
[updateId] => 1
[eventId] => 1
)
[1] => Array
(
[updateId] => 2
[eventId] => 1
)
)
)
[1] => Array
(
[eventId] => 2
[updates] => Array
(
[0] => Array
(
[updateId] => 3
[eventId] => 2
)
)
)
)
Upvotes: 0
Reputation: 16688
It seems very inefficient code to me, although I cannot see what the problem it. It would be nice if the key of the $L_a_events was the eventId, then you could start from the updates you need to add:
foreach ($L_a_updates as $update) {
$eventId = $update['eventId'];
$L_a_events[$eventId]['updates'][] = $update;
}
which would be so much simpler and quicker. The assumption is that an event exists if you have an update for it. You could even write a bit of code to make it so:
foreach ($L_a_events => $event)
{
$eventId = $event['eventId'];
$newEvents[$eventId] = $event;
}
And use $newEvents instead of $L_a_events. Now you don't have nested loops anymore.
Upvotes: 1
Reputation: 9311
You don't need two nested foreach loops to do this. What you need to do is:
for each update:
if event with this update's event id exists:
add update to the event's array entry
There is absolutely no need to loop over all events. You already have an array that contains all those events. All you need to do is make sure that your event array uses the event's ID as key.
In PHP this would look something like this:
foreach ($L_a_updates as $update) {
$key = $update['event_id'];
if (array_key_exists($key, $L_a_events)) {
$L_a_events[$key]['updates'][] = $update;
}
}
Upvotes: 1