0

EDIT


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.

Original question below


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',
              )
          )
      )
 );
9
  • What's the result can you please post it Commented Feb 27, 2015 at 13:23
  • Can you post the two arrays' structures please? Commented Feb 27, 2015 at 13:24
  • Nothing sticks out here, can you show us an example - perhaps in a PHP fiddle? Commented Feb 27, 2015 at 13:24
  • Why don't you merge your SQL statements and JOIN those two tables together? Traversing the result is most likely easier than doing this manual merge. Commented Feb 27, 2015 at 13:25
  • I'll do a fiddle in a bit, gonna take a while because I can only reproduce this problem on the production environment for some reason. Commented Feb 27, 2015 at 13:26

3 Answers 3

1

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;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, you had the same idea. :-)
You know I've been thinking the nested loops are the problem. But when I use your example, something else goes wrong. I think the real problem here probably is that it's a Friday... ffs.
1

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.

Comments

0

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
                        )

                )

        )

)

3 Comments

Like I said a bunch of times. I did check the data. I checked the database. I checked the result set that came out of the database. There are no duplicates. Period.
I didn't mean to ignore your comment. The code i posted proves that your logic works. Hence, i can only think that the problem is with your input arrays.
Yeah I don't see why it wouldn't work either. I mean it's ugly, but... I feel like this is one of those cases where I should look at it tomorrow and probably see what the problem was straight away. Have a dirty fix at least, for now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.