PHP 2D array finding values by certain criteria

Each item of PHP 2D array $g contains

  • ID in index 0,
  • delay in index 5,
  • binary in index 6,
  • category in index 7,
  • weight in index 8

For each ID show cat for binary 1 having highest delay along with weight and category for binary 0 having highest delay along with weight.

If multiple categories have delay within 60 between each other, chose the one where weight for binary 0 and 1 for the same category and delay are closest to each other. The categories shown for 0 and 1 must be the same.

Example data

<?php

$g[0]=array('18572','1713654000','751','759','aB3cD','14','0','38','160','1');
$g[1]=array('18572','1713654000','751','759','eF4gH','14','0','40','169','1');
$g[2]=array('18572','1713654000','751','759','J7kLm','14','0','42','182','1');
$g[3]=array('18572','1713654000','751','759','N8oPq','14','0','44','199','1');
$g[4]=array('18572','1713654000','751','759','R1sTu','14','0','46','217','1');
$g[5]=array('18572','1713654000','751','759','vW9xY','14','0','48','237','1');
$g[6]=array('18572','1713654000','751','759','zZ2aB','14','0','50','252','1');
$g[7]=array('18572','1713654000','751','759','6C7dE','25056','0','38','157','1');
$g[8]=array('18572','1713654000','751','759','fG8hI','25056','0','42','177','1');
$g[9]=array('18572','1713654000','751','759','L3mNo','25056','0','44','193','1');
$g[10]=array('18572','1713654000','751','759','pQ4rS','25056','0','46','210','1');
$g[11]=array('18572','1713654000','751','759','T5uVw','25056','0','48','229','1');
$g[12]=array('18572','1713654000','751','759','xY6zA','25056','0','50','244','1');
$g[13]=array('18572','1713654000','751','759','B7cDe','25057','0','40','165','1');
$g[14]=array('18572','1713654000','751','759','F9gHj','14','1','38','235','1');
$g[15]=array('18572','1713654000','751','759','Q2rSt','14','1','40','219','1');
$g[16]=array('18572','1713654000','751','759','V4wXy','14','1','42','201','1');
$g[17]=array('18572','1713654000','751','759','Z5aBc','14','1','44','184','1');
$g[18]=array('18572','1713654000','751','759','gH6iJ','14','1','46','170','1');
$g[19]=array('18572','1713654000','751','759','kL7mN','14','1','48','159','1');
$g[20]=array('18572','1713654000','751','759','oP8qR','14','1','50','153','1');
$g[21]=array('18572','1713654000','751','759','rS9tU','25056','1','38','242','1');
$g[22]=array('18572','1713654000','751','759','vW1xY','25056','1','42','207','1');
$g[23]=array('18572','1713654000','751','759','zX2aB','25056','1','44','189','1');
$g[24]=array('18572','1713654000','751','759','D3eFg','25056','1','46','175','1');
$g[25]=array('18572','1713654000','751','759','E4fGh','25056','1','48','163','1');
$g[26]=array('18572','1713654000','751','759','F5gHi','25056','1','50','156','1');
$g[27]=array('18572','1713654000','751','759','G6hIj','25057','1','40','226','1');
$g[28]=array('18573','1713654000','755','745','H7jK8','23734','0','58','224','1');
$g[29]=array('18573','1713654000','755','745','I8kL9','23734','0','60','251','1');
$g[30]=array('18573','1713654000','755','745','J9lM0','23792','0','54','195','1');
$g[31]=array('18573','1713654000','755','745','K0mN1','25057','0','48','156','1');
$g[32]=array('18573','1713654000','755','745','L1nO2','25057','0','50','167','1');
$g[33]=array('18573','1713654000','755','745','M2oP3','25057','0','52','179','1');
$g[34]=array('18573','1713654000','755','745','N3pQ4','25057','0','54','194','1');
$g[35]=array('18573','1713654000','755','745','O4qR5','25057','0','56','210','1');
$g[36]=array('18573','1713654000','755','745','P5rS6','25057','0','58','224','1');
$g[37]=array('18573','1713654000','755','745','Q6sT7','25057','0','60','251','1');
$g[38]=array('18573','1713654000','755','745','R7tU8','24938','1','48','243','1');
$g[39]=array('18573','1713654000','755','745','S8uV9','24938','1','52','204','1');
$g[40]=array('18573','1713654000','755','745','T9vW0','24938','1','54','187','1');
$g[41]=array('18573','1713654000','755','745','U0wX1','25057','1','48','244','1');
$g[42]=array('18573','1713654000','755','745','V1xY2','25057','1','50','222','1');
$g[43]=array('18573','1713654000','755','745','W2yZ3','25057','1','52','205','1');
$g[44]=array('18573','1713654000','755','745','X3zA4','25057','1','54','188','1');
$g[45]=array('18573','1713654000','755','745','Y4aB5','25057','1','56','175','1');
$g[46]=array('18573','1713654000','755','745','Z5bC6','25057','1','58','166','1');
$g[47]=array('18573','1713654000','755','745','a6C7d','25057','1','60','153','1');
$g[48]=array('18574','1713657600','749','817','b8D9e','28656','0','32','149','1');
$g[49]=array('18574','1713657600','749','817','c9E0f','28656','0','34','161','1');
$g[50]=array('18574','1713657600','749','817','d0F1g','28656','0','36','173','1');
$g[51]=array('18574','1713657600','749','817','e1G2h','28656','0','38','191','1');
$g[52]=array('18574','1713657600','749','817','f2H3i','28656','0','40','206','1');
$g[53]=array('18574','1713657600','749','817','g3I4j','28656','0','42','221','1');
$g[54]=array('18574','1713657600','749','817','h4J5k','28656','0','44','250','1');
$g[55]=array('18574','1713657600','749','817','i5K6l','28656','1','32','262','1');

The code below works well for highest delay, but for lowest returns the same. How should I modify the code to work correctly for lowest as well? E. g. for the ID 18572 the output should be Lowest Delay: cat: 44, Outcome 0: 184, Outcome 1: 199, Delay: 14


$g = array ( /* see example data above */);
$groupedById = [];

foreach ($g as $entry) {
    $id = $entry[0];
    if (!isset($groupedById[$id])) {
        $groupedById[$id] = [];
    }
    $groupedById[$id][] = $entry;
}

foreach ($groupedById as $id => $entries) {
    $bin0 = [];
    $bin1 = [];

    foreach ($entries as $entry) {
        if ($entry[6] == '0') {
            $bin0[] = $entry;
        } elseif ($entry[6] == '1') {
            $bin1[] = $entry;
        }
    }

    usort($bin0, fn($a, $b) => $b[5] - $a[5]);
    usort($bin1, fn($a, $b) => $b[5] - $a[5]);

    $highest = getBestMatch($bin0, $bin1);
    $lowest = getBestMatch(array_reverse($bin0), array_reverse($bin1));

    echo "ID: $idn";
    echo "Highest Delay:n";
    echo "cat: {$highest['cat']}, Bin 0: {$highest['$w0']}, Bin 1: {$highest['$w1']}, Delay: {$highest['delay']}n";
    echo "Lowest Delay:n";
    echo "cat: {$lowest['cat']}, Bin 0: {$lowest['$w0']}, Bin 1: {$lowest['$w1']}, Delay: {$lowest['delay']}n";
}

function getBestMatch($bin0, $bin1) {
    $bestMatch = null;
    $minwDiff = PHP_INT_MAX;

    foreach ($bin0 as $o0) {
        foreach ($bin1 as $o1) {
            if ($o0[7] == $o1[7] && abs($o0[5] - $o1[5]) <= 60) {
                $wDiff = abs($o0[8] - $o1[8]);
                if ($wDiff < $minwDiff) {
                    $minwDiff = $wDiff;
                    $bestMatch = [
                        'cat' => $o0[7],
                        '$w0' => $o0[8],
                        '$w1' => $o1[8],
                        'delay' => max($o0[5], $o1[5]),
                    ];
                }
            }
        }
    }

    return $bestMatch;
}

?>