1

如果它们之间的时间少于 20 分钟,我如何从数组中打印出我的值?

This is my foreach for myArray:

     foreach ($cooltransactions as $key=>$value)
{
$array[$key] = array('cardid' => $cooltransactions[$key]['cardid'],'created' => strtotime($cooltransactions[$key]['created']));     
 }
$newArray=array();
$i=0;
foreach($array as $k=>$v){
       $id=$array[$k]['cardid'];
       $time=$array[$k]['created'];
       foreach($array as $k1=>$v1){
          if($k<$k1){
             if($array[$k1]['cardid']==$id){
                 if(compare($time,$array[$k1]['created'])){
                     $newArray[$i]['cardid']=$id;
                     $newArray[$i]['created']=date('d-m-y H:i:s', $time['created']);
                     $i++;
                 }
             }
          }
       }
    }

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

print_r($newArray);

这就是我得到的:

Array ( 
[0] => Array ( [cardid] => 56324  [created] => 01-01-70 01:00:00)
[1] => Array ( [cardid] => 56324  [created] => 01-01-70 01:00:00) 
[2] => Array ( [cardid] => 21352 [created] => 01-01-70 01:00:00) 
[3] => Array ( [cardid] => 21352 [created] => 01-01-70 01:00:00)
)

但这是我想要的输出:

Array  ( 
    [1] => Array ( [Cardid] => 21352 [created] => 02-02-13 11:11:41 ) 
    [2] => Array ( [Cardid] => 21352 [created] => 02-02-13 10:59:48 )     
  )
4

3 回答 3

0

似乎您希望根据条件拆分数组,因此您可以使用 foreach 并使用第一个变量设置一个临时变量并对其进行迭代,然后在 if 语句的帮助下使用临时值检查当前节点的时间。

于 2013-02-14T09:39:20.950 回答
0

使用此代码:

 $cooltransactions=Array ( 
    Array ( 'Cardid' => 56324, 'created' => '11-02-13 15:36:32' ),
    Array ( 'Cardid' => 56324, 'created' => '11-02-13 15:08:51' ),         
    Array ( 'Cardid' => 42683, 'created' => '10-02-13 13:42:24' ), 
    Array ( 'Cardid' => 21352, 'created' => '02-02-13 11:11:41' ), 
    Array ( 'Cardid' => 21352, 'created' => '02-02-13 11:11:41' ),
    Array ( 'Cardid' => 74124, 'created' => '02-02-13 11:02:53' ), 
    Array ( 'Cardid' => 21352, 'created' => '02-02-13 10:59:48' ) 
 );

foreach ($cooltransactions as $key=>$value)
{
$array[$key] = array('Cardid' => $cooltransactions[$key]['Cardid'],
                     'created' => strtotime($cooltransactions[$key]['created'])
                    );     
 }
$newArray=array();
$i=0;
foreach($array as $k=>$v){
    if(isset($array[$k])){
       $id=$array[$k]['Cardid'];
       $time=$array[$k]['created'];
        echo $time.'<br>';
       foreach($array as $k1=>$v1){
          if($k<$k1){
             if($array[$k1]['Cardid']==$id){
                 echo '2 fields with the same id have been detected: id='.$id.'</br>'; 
                 if(compare($time,$array[$k1]['created'])){
                     $newArray[$i]['Cardid']=$id;
                     $newArray[$i]['created']=date("Y-m-d h:i:s", $array[$k1]['created']);
                     unset($array[$k1]);
                     $i++;
                 }
             }
          }
       }
    }
 }
echo '<br>FINAL ARRAY:<br>';
var_dump($newArray);


  function compare($firsttime, $secondtime){
     $interval = $firsttime-$secondtime;
     $dif=round(abs($interval) / 60);
     echo 'This fields have the same id: the difference in minutes is: '.$dif.' </br>';
     if ($dif < 20 || $dif < -20 ) {
        return true;
     }
  }

1297629392 已检测到 2 个 id 相同的字段:id=56324 此字段具有相同 id:分钟差异为:28 1297627731 1266086544 1013616701 已检测到 2 个具有相同 id 的字段:id=21352 此字段具有相同 id : 分钟差是: 0 检测到 2 个 id 相同的字段: id=21352 这个字段有相同 id: 分钟差是: 12 1013616173

最终数组:数组(2){[0]=>数组(2){[“Cardid”]=> int(21352)[“created”]=>字符串(19)“2002-02-13 11:11: 41" } [1]=> array(2) { ["Cardid"]=> int(21352) ["created"]=> string(19) "2002-02-13 10:59:48" } }

于 2013-02-14T09:39:23.130 回答
0
$arr=Array ( 
    Array ( 'Cardid' => 56324, 'created' => '11-02-13 15:36:32' ),
    Array ( 'Cardid' => 56324, 'created' => '11-02-13 15:08:51' ),         
    Array ( 'Cardid' => 42683, 'created' => '10-02-13 13:42:24' ), 
    Array ( 'Cardid' => 21352, 'created' => '02-02-13 11:11:41' ), 
    Array ( 'Cardid' => 74124, 'created' => '02-02-13 11:02:53' ), 
    Array ( 'Cardid' => 21352, 'created' => '02-02-13 10:59:48' ) 
 );

function filter_rows($rows)
{
   $cards=array();
   foreach($rows as $row)
   {
     $cards[$row['Cardid']][]=$row['created'];
   }

   $res=array();

   foreach($cards as $cardid=>$card_trns)
   {
      for($i=1,$ms=count($card_trns);$i<$ms;$i++)
      {
        $d1=new DateTime($card_trns[$i]);
        $d0=new DateTime($card_trns[$i-1]);
        $di=$d1->diff($d0,true);
        $diff_min=$di->format('%i');
        if($diff_min<=20)
        {
           $res[]=array('Cardid'=>$cardid, 'created'=>$card_trns[$i]);
           $res[]=array('Cardid'=>$cardid, 'created'=>$card_trns[$i-1]);
        }
      }
   }
   return $res;
}

print_r(filter_rows($arr));
于 2013-02-14T10:26:33.120 回答