dtkmejg127475 2013-12-05 11:06
浏览 31

如何操作我的阵列,所以如果在60分钟内扫描相同

This is really hard to explain, but i will do my best.

I have a array called arrayoftransationcs which contains 4 strings. *SCAN, MEMBER_ID, RESTAURANT, TIME, PAID. If a scan is paid, it will defined as 1, and if not it will have 0.

So the main question to my problem is, how can I get scan that contain paid = 0 which has not have paid = 1 within 60 min. I will give an example.

Example A In this example, there is 3 scan which comes from the same cafe x within 20 min. The first 2 has been defined as paid = 0, and the last has been defined as paid = 1. Because there is paid = 1 within 60min with the same member_id and restaurant, i dont want any of the scan

Scan A  1635752   Cafe X    17-11-2013 21:00    Paid 1  
Scan B  1635752   Cafe X    17-11-2013 20:50    Paid 0  
Scan C  1635752   Cafe X    17-11-2013 20:40    Paid 0

Example B In this example, there is also 3 scan from the same member, same restaurant, within 60min, but all of them has been defined as paid = 0. Because there is no paid = 1 in this example, i want these in a array. This is the goal. But there is a twist here, because there is more than 1 scan to use, i only want the latest scan in this example, which will mean only scan A can be used

Scan A  1635752   Cafe X    17-11-2013 21:00    Paid 0  
Scan B  1635752   Cafe X    17-11-2013 20:50    Paid 0  
Scan C  1635752   Cafe X    17-11-2013 20:40    Paid 0  

So I hope you understand the my question. I need paid = 0 scan if there is not a paid = 1 scan subsequently with the same member_id, restaurant within 60min

This is how i tried.

I am looping my array 2 times, and then checking for it the same member_id (cardid) and restaurants are equals together, if yes, then check time. If it is whitin 60min, mark the scan as double

 foreach($arrayoftransationcs as $key => $array)
  {
    $time=$arrayoftransationcs[$key]['created'];
    $cardid = $arrayoftransationcs[$key]['cardid'];
    $restaurant_id = $arrayoftransationcs[$key]['restaurant_id'];

    if(isset($arrayoftransationcs[$key]))
    {
        foreach($arrayoftransationcs as $k1=>$v1)
        {
            $time2=$arrayoftransationcs[$k1]['created'];

            if($key<$k1)
            {
                if($arrayoftransationcs[$k1]['cardid']==$cardid && $arrayoftransationcs[$k1]['restaurant_id']==$restaurant_id)
                {
                    if(compare($time,$time2))
                    {   

                        $arrayoftransationcs[$key]['error'] = 'double';
                        $arrayoftransationcs[$k1]['error'] = 'double';                         
                    }

                }
            }
        }
    }   
  }

checking the time here.

 function compare($firsttime, $secondtime)
 {
     $interval = $firsttime-$secondtime;
     $dif=round(abs($interval) / 60);
     if ($dif < 60 || $dif < -60 ) 
     {
        if ($dif!==0) 
        {
             return true;
        }
        else
        {
            return false;
        }
     }
   }

This is the place, where i filter after paid = 0 and does not contain anything in the error field.

  foreach ($arrayoftransationcs as $key) 
  {
    if($key['paid'] == 0 && empty($key['error']))
    {
            $ids[] = $key['transactionid'];

     }
  }

But I am not sure it is the right approach, I did it with the code. For technically, I select all scan which has the same membership number, restaurant with "double" in the field error, if it is paid = 1 or 0, does not matter .. this is not right, i think.

So I'm missing something here, checking if there is paid = 0 scan here if yes, check if there is a paid = 1 scan within the next 60min having the same medlem_id and restaurant. If so, then mark them as doubles.

  • 写回答

3条回答 默认 最新

  • duanbei6427 2013-12-05 11:46
    关注

    This is how you do it:

    <?php
    
    // Data Source
    $arrayoftransationcs = array(
        array(
            'transactionid' => 16148,
            'cardid' => 10010234,
            'created' => 1380650784,
            'restaurant_id' => 32089,
            'paid' => 1
        ),
        array(
            'transactionid' => 16552,
            'cardid' => 10010241,
            'created' => 1381522288,
            'restaurant_id' => 41149,
            'paid' => 1
        ),
        array(
            'transactionid' => 16936,
            'cardid' => 10010440,
            'created' => 1386247655,
            'restaurant_id' => 47897,
            'paid' => 0
        ),
        array(
            'transactionid' => 16808,
            'cardid' => 10010557,
            'created' => 1382361447,
            'restaurant_id' => 43175,
            'paid' => 0
        ),
        array(
            'transactionid' => 18932,
            'cardid' => 10010440,
            'created' => 1386247655,
            'restaurant_id' => 47897,
            'paid' => 1
        )
    );
    
    // Helper Function
    function getUnpaidWithinHour($transactions) {
        $unpaid_transactions = array();
        $time_now = time();
        foreach ($transactions as $transaction) {
            if ($transaction['paid']) continue;
            if (($time_now - $transaction['created']) < 3600 && !$transaction['paid']) {
                $unpaid_transactions[] = $transaction;
            }
        }
        return $unpaid_transactions;
    }
    
    // Test
    $unpaid_transactions = getUnpaidWithinHour($arrayoftransationcs);
    echo "<pre>";
    print_r($unpaid_transactions);
    echo "<pre>";
    
    ?>
    

    Outputs:

    Array
    (
        [0] => Array
            (
                [transactionid] => 16936
                [cardid] => 10010440
                [created] => 1386247655
                [restaurant_id] => 47897
                [paid] => 0
            )
    
    )
    

    To test if this is working, I edited the transaction 16936 to have a timestamp of 5 minutes ago and ran the function. The code correctly detected that transaction.

    Try it yourself and with your own datasource.

    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法