如何操作我的阵列,所以如果在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.

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.

Let's make things simple, your question is: Find paid = 0 scan if there is not paid = 1 with same member_id within 60min, since lack of test data, so i'll describe below with nature language in php style:

make an empty arrayPaid

foreach (all data) {
    if time passed greater than 60min, continue;

    if paid
        add member_id in arrayPaid: arrayPaid[member_id] = something
        continue;

    if unpaid and arrayPaid[member_id] is not set
        this is the data you want
}

The logic is clear, easy to write your real code. And one important thing is, your data need to be order by time in desc.

I found the answer by doing this.

 foreach ($results->result_array() as $filterofresults) 
  {
    if($filterofresults['paid'] == 1)
    {
      $arrayoftransationcs1[]=$filterofresults;

    }
        if($filterofresults['paid'] == 0)
    {
      $arrayoftransationcs[]=$filterofresults;

    }
  }

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

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

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

                        $arrayoftransationcs[$key]['error'] = 'Dobbelt';
                        $arrayoftransationcs1[$k1]['error'] = 'Dobbelt';  

                    }

                }
            }
        }
    }   
  }
  foreach ($arrayoftransationcs as $key) 
  {

    if($key['paid'] == 0 && empty($key['error']))
    {
            $samlet[] = get_all_data($key['transactionid']);
     }
  }