Сравнение нескольких массивов для одинаковых значений

У меня есть следующий код, который производит массив.

Мне нужно разделить этот массив и сравнить полученные массивы друг с другом.

Мне нужно только сравнить массивы, которые находятся рядом друг с другом по порядку.

Итак, первое и второе, второе и третье и т. Д. Я могу создать еще один массив для хранения порядка, который необходимо сравнить, если это поможет. Я просто понятия не имею, как их проверить.

Цель состоит в том, чтобы массивы по обе стороны друг от друга не содержали одинаковый $ 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
)
)
)

4

Решение

Вы можете добиться этого, сравнивая текущий идентификатор гонщика со следующим идентификатором гонщика в массиве …

<?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)
),
);

Цикл для сравнения идентификаторов гонщика

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 идентификатор гонщика (493) в индексах 2 и 3 одинаков в классе 17

LIVE DEMO ДЛЯ ЭТОГО

$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
)

)

)

Найти дубликат идентификатора в двух смежных классах

$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
}
}
}
}

Живой пример

3

Другие решения

Не уверен в 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?'
}
}
}
2

По вопросам рекламы [email protected]