比较相同值的多个数组

Posted

技术标签:

【中文标题】比较相同值的多个数组【英文标题】:Comparing multiple arrays for the same values 【发布时间】:2016-09-11 18:52:36 【问题描述】:

我有以下生成数组的代码。

我需要拆分该数组并将生成的数组相互比较。

我只需要按顺序比较彼此相邻的数组。

所以第一个和第二个,第二个和第三个,依此类推。如果有帮助,我可以构建另一个数组来存储需要比较的顺序。我只是不知道如何像这样检查它们。

目标是确保彼此两侧的数组不包含相同的 $racer_id

它是用于赛道的计分应用程序,一个赛车手不能参加一个接一个的比赛。

谁能给我一个正确的方向?

** 只有一个 $event_id,有多个 $class_id 和 $racer_id。

<?php
function getEventClasses()

    global $db;
    global $event_id;
    try 
        $getEventClasses = $db->prepare('SELECT class_id
                                     FROM tbl_event_classes
                                     WHERE event_id = :event_id
                                     ORDER BY sort');
        $getEventClasses->bindValue(':event_id', $event_id);
        $getEventClasses->execute();
     catch (PDOException $ex) 
        echo 'An Error occurred!<br>';
        //user friendly message
        $ex->getMessage();
        echo $ex;
        exit;
    
    $eventClasses = [];
    while ($r = $getEventClasses->fetch(PDO::FETCH_ASSOC)) 
        $eventClasses[] = [
            'class_id' => $r['class_id']
        ];
    
    return $eventClasses;


function getEntries($class_id)

    global $db;
    global $event_id;
    try 
        $getEntries = $db->prepare('SELECT tbl_event_entries.racer_id, 
                                   tbl_event_entries.class_id, 
                                   tbl_event_entries.event_id, 
                                   tbl_event_classes.class_type, 
                                   tbl_event_classes.points_class, 
                                   tbl_moto_order.moto_nbr
                            FROM tbl_moto_order 
                            INNER JOIN (tbl_event_entries 
                             LEFT JOIN tbl_event_classes 
                                    ON (tbl_event_entries.event_id = tbl_event_classes.event_id)
                                    AND (tbl_event_entries.class_id = tbl_event_classes.class_id)) 
                                    ON (tbl_moto_order.class_id = tbl_event_entries.class_id)
                                    AND (tbl_moto_order.event_id = tbl_event_entries.event_id)
                            WHERE tbl_event_entries.event_id = :event_id
                            AND tbl_event_entries.racer_id != "9999"
                            AND tbl_event_entries.class_id = :class_id');
        $getEntries->bindValue(':event_id', $event_id);
        $getEntries->bindValue(':class_id', $class_id);
        $getEntries->execute();
     catch (PDOException $ex) 
        echo 'An Error occurred!<br>';
        //user friendly message
        $ex->getMessage();
        echo $ex;
        exit;
    
    $eventEntries = [];
    while ($r = $getEntries->fetch(PDO::FETCH_ASSOC)) 
        $eventEntries[] = [
            'racer_id'     => $r['racer_id'],
            'class_id'     => $r['class_id'],
            'event_id'     => $r['event_id'],
            'class_type'   => $r['class_type'],
            'points_class' => $r['points_class'],
            'moto_nbr'     => $r['moto_nbr']
        ];
    
    return $eventEntries;


$eventClasses = getEventClasses();
foreach ($eventClasses as $eventClass) 
    $eventEntries = getEntries($eventClass['class_id']);
    foreach ($eventEntries as $eventEntry) 
        $runningClasses[$eventClass['class_id']][] = [
            'racer_id'     => $eventEntry['racer_id'],
            'event_id'     => $eventEntry['event_id'],
            'class_type'   => $eventEntry['class_type'],
            'points_class' => $eventEntry['points_class'],
            'moto_nbr'     => $eventEntry['moto_nbr']
        ];
    

foreach ($runningClasses as $class) 
    $f->printr($class);

?>

数组:

     Array
 (
     [17] => Array
         (
             [0] => Array
                 (
                     [racer_id] => 95
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 17
                     [moto_nbr] => 3
                 )
             [1] => Array
                 (
                     [racer_id] => 350
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 17
                     [moto_nbr] => 3
                 )
             [2] => Array
                 (
                     [racer_id] => 493
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 17
                     [moto_nbr] => 3
                 )
             [3] => Array
                 (
                     [racer_id] => 295
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 17
                     [moto_nbr] => 3
                 )
             [4] => Array
                 (
                     [racer_id] => 1142
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 17
                     [moto_nbr] => 3
                 )
             [5] => Array
                 (
                     [racer_id] => 628
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 17
                     [moto_nbr] => 3
                 )
         )
     [14] => Array
         (
             [0] => Array
                 (
                     [racer_id] => 1142
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 14
                     [moto_nbr] => 4
                 )
             [1] => Array
                 (
                     [racer_id] => 162
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 14
                     [moto_nbr] => 4
                 )
             [2] => Array
                 (
                     [racer_id] => 151
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 14
                     [moto_nbr] => 4
                 )
             [3] => Array
                 (
                     [racer_id] => 1176
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 14
                     [moto_nbr] => 4
                 )
             [4] => Array
                 (
                     [racer_id] => 401
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 14
                     [moto_nbr] => 4
                 )
             [5] => Array
                 (
                     [racer_id] => 264
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 14
                     [moto_nbr] => 4
                 )
             [6] => Array
                 (
                     [racer_id] => 797
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 14
                     [moto_nbr] => 4
                 )
             [7] => Array
                 (
                     [racer_id] => 124
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 14
                     [moto_nbr] => 4
                 )
             [8] => Array
                 (
                     [racer_id] => 29
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 14
                     [moto_nbr] => 4
                 )
             [9] => Array
                 (
                     [racer_id] => 74
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 14
                     [moto_nbr] => 4
                 )
             [10] => Array
                 (
                     [racer_id] => 117
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 14
                     [moto_nbr] => 4
                 )
             [11] => Array
                 (
                     [racer_id] => 408
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 14
                     [moto_nbr] => 4
                 )
             [12] => Array
                 (
                     [racer_id] => 501
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 14
                     [moto_nbr] => 4
                 )
             [13] => Array
                 (
                     [racer_id] => 1125
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 14
                     [moto_nbr] => 4
                 )
         )
     [8] => Array
         (
             [0] => Array
                 (
                     [racer_id] => 1180
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 8
                     [moto_nbr] => 6
                 )
         )
     [16] => Array
         (
             [0] => Array
                 (
                     [racer_id] => 73
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 16
                     [moto_nbr] => 12
                 )
             [1] => Array
                 (
                     [racer_id] => 9
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 16
                     [moto_nbr] => 12
                 )
             [2] => Array
                 (
                     [racer_id] => 525
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 16
                     [moto_nbr] => 12
                 )
         )
     [13] => Array
         (
             [0] => Array
                 (
                     [racer_id] => 1089
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 13
                     [moto_nbr] => 15
                 )
             [1] => Array
                 (
                     [racer_id] => 73
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 13
                     [moto_nbr] => 15
                 )
             [2] => Array
                 (
                     [racer_id] => 162
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 13
                     [moto_nbr] => 15
                 )
             [3] => Array
                 (
                     [racer_id] => 264
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 13
                     [moto_nbr] => 15
                 )
             [4] => Array
                 (
                     [racer_id] => 797
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 13
                     [moto_nbr] => 15
                 )
             [5] => Array
                 (
                     [racer_id] => 422
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 13
                     [moto_nbr] => 15
                 )
             [6] => Array
                 (
                     [racer_id] => 1176
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 13
                     [moto_nbr] => 15
                 )
             [7] => Array
                 (
                     [racer_id] => 76
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 13
                     [moto_nbr] => 15
                 )
             [8] => Array
                 (
                     [racer_id] => 1136
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 13
                     [moto_nbr] => 15
                 )
         )
     [5] => Array
         (
             [0] => Array
                 (
                     [racer_id] => 600
                     [event_id] => 326
                     [class_type] => Y
                     [points_class] => 5
                     [moto_nbr] => 16
                 )
             [1] => Array
                 (
                     [racer_id] => 1143
                     [event_id] => 326
                     [class_type] => Y
                     [points_class] => 5
                     [moto_nbr] => 16
                 )
         )
     [10] => Array
         (
             [0] => Array
                 (
                     [racer_id] => 421
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 10
                     [moto_nbr] => 19
                 )
             [1] => Array
                 (
                     [racer_id] => 1219
                     [event_id] => 326
                     [class_type] => A
                     [points_class] => 10
                     [moto_nbr] => 19
                 )
         )
 )

【问题讨论】:

你能详细说明下一个race_track是什么? 你好,@indymx 你想在一个班级 ID 中比较赛车手 ID。例如第一个类 id 17 有 4 个子条目,所以你想将这四个相互比较,以此类推,以获得下一个类 id 的......?? 胡安赛道是摩托车比赛的设施。 曼吉特。我只关心比较当前“类”的“class_id”数组和紧随其后的数组。我将添加一个“排序”字段,如果测试中有重复的“racer_id”,则需要更改该字段。 【参考方案1】:

第一个答案

您可以通过将当前赛车手 id 与数组中的下一个赛车手 id 进行比较来实现这一点...

<?php 
$InputArray = array(
            17=>array(
                        0=>array(0=>95,1=>2 ),
                        1=>array(0=>350,1=>3),
                        2=>array(0=>493,1=>4),
                        3=>array(0=>493,1=>4)
                    ),
            18=>array(
                        0=>array(0=>56,1=>2 ),
                        1=>array(0=>89,1=>3),
                        2=>array(0=>52,1=>4)
                    ),
             );

用于比较赛车手 ID 的循环

foreach($InputArray as $key => $val)

    foreach($val as $k => $v)
    
        $nextKey =$k+1;
        if(array_key_exists($nextKey,$val))
        
            if($v[0]==$val[$nextKey][0])
            
                    echo "Racer ID ".$v[0]." Mathced in Class ID : ".$key;
                    ////sucess code here if same race id's are there...
            
        
    

?>

这将 O/P

Racer ID 493 Mathced in Class ID : 17

$InputArray 赛车手 id(493) 中的索引 2 和 3 在类 id 17 中是相同的

LIVE DEMO FOR THIS

修改后的答案

$InputArray = array(
                17=>array(
                            0=>array('racer_id'=>95,'event_id'=>326 ),
                            1=>array('racer_id'=>350,'event_id'=>326),
                            2=>array('racer_id'=>493,'event_id'=>326),
                            3=>array('racer_id'=>1142,'event_id'=>326)
                        ),
                14=>array(
                            0=>array('racer_id'=>1142,'event_id'=>326),
                            1=>array('racer_id'=>162,'event_id'=>326),
                            2=>array('racer_id'=>151,'event_id'=>326)
                        ),
                 );

数组看起来像:

Array
(
    [17] => Array
        (
            [0] => Array
                (
                    [racer_id] => 95
                    [event_id] => 326
                )

            [1] => Array
                (
                    [racer_id] => 350
                    [event_id] => 326
                )

            [2] => Array
                (
                    [racer_id] => 493
                    [event_id] => 326
                )

            [3] => Array
                (
                    [racer_id] => 1142
                    [event_id] => 326
                )

        )

    [14] => Array
        (
            [0] => Array
                (
                    [racer_id] => 1142
                    [event_id] => 326
                )

            [1] => Array
                (
                    [racer_id] => 162
                    [event_id] => 326
                )

            [2] => Array
                (
                    [racer_id] => 151
                    [event_id] => 326
                )

        )

)   

在两个相邻的类中查找重复的 Id

$keys = array_keys($InputArray);///this will give you an array of all the class id's 17,14,8...      
foreach($InputArray as $key => $val)

    $racer_ids = array_map(function ($ar) return $ar['racer_id'];, $val);///get all the racers from current class
    $currentKey = array_search($key,$keys); ///get the key for current class in keys array
    if(array_key_exists($currentKey+1 ,$keys)) //check if next key exist in keys array (key for 14)
     
        $nextKey = $keys[$currentKey+1]; /// value present at next key in keys array 14 is the next key
        if(array_key_exists($nextKey,$InputArray)) ///check if next key present in $InputArray
        
            $next_racer_ids = array_map(function ($ar) return $ar['racer_id'];, $InputArray[$nextKey]);///get all the racers from next class
            $result = array_intersect($next_racer_ids,$racer_ids);///compare racer ids from both array's  
            if(!empty($result)) ////return empty if racer_id is not same 
            
                echo "SAME DRIVER";
                //your success code here
            
        
    

LIVE EXAMPLE

【讨论】:

以后有机会我会试试这个的。 这很接近,但解析数组的深度不够。您的数组缺少一层。 怎么样??正如您在 id 17 类中看到的那样,array_key_exists() 三次为真,它比较了 0-1、1-2 和 2-3 的赛车手 id @indymx,现在看看代码,我的数据集与你的相似......以前我使用索引'racer_id',但在你的数组中你没有使用任何索引,这就是为什么这令人困惑...... @indymx 现在看看我的代码,试试这个,我想这就是你要找的,..【参考方案2】:

在 PHP 中不确定。但是像这样的

  for i = 0 to $eventClasses.count - 1 
   
      $event = $eventClasses[i];

      for j = 0 to $event.count -2 // last race doesnt compare with next
      
          $currentEvent = $event[j]
          $nextEvent    = $event[j+1];

          if ($currentEvent[0] == $nextEvent[0] ) 
          
              echo 'Same driver on next race?'
          
      
  

【讨论】:

不确定一个赛事的最后一场比赛是否必须与下一个赛事的第一场比赛进行比较 event_id 保持不变

以上是关于比较相同值的多个数组的主要内容,如果未能解决你的问题,请参考以下文章

Algs4-2.1.28相等的主键

Alamofire 发送带键和多值的参数数组

查找最大值比较每个索引的多个数组

ActionScript 3.0 - 获取数组中相同值的计数

数组对象修改某一个属性值的数据类型

PySpark - 获取具有相同值的数组元素的数量