doutangguan2460 2015-02-27 13:19
浏览 66

Loop将单个元素添加到数组两次

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',
              )
          )
      )
 );
  • 写回答

3条回答 默认 最新

  • duanao2688 2015-02-27 13:29
    关注

    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;
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法