title: How to search data in a csv for a substring in a specific column and only extract the match with the most recent date?
body: <p>I'm facing a problem when trying to get the recent date for a car plate inside of a text file.</p>
  
  <p>What I want to do is find the row with the most recent date value (value index [6]) containing <code>DPC-5345</code> at the start of the plate value (value index [9]).</p>
  
  <p>What I have done in the following code is receive the first matching record on file, but it is not necssarily the most recent date for that plate.</p>
  
  <pre><code>$filename = "posicoes.txt";
  $fi = fopen($filename, "r+");
  $getarray = array();
  $row = 1;
  if (($handle = $fi) !== FALSE) {
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
          $num = count($data);
          $row++;
          for ($c=0; $c &lt; $num; $c++) {
              echo $data[$c] . "&lt;br /&gt;\n";
              $placa = mb_substr($data[9], 0, 8);
              $getarray[] = array($data[4],$data[5],$data[6],$data[8],$placa);
          }
      }
      fclose($handle);
  }
  $i=1; 
  while($i&lt;=count($getarray)) {
      if ($getarray[$i][4] == 'DPC-5345') {
          break;
      } 
      $i++;
  }
  </code></pre>
  
  <p>Is there anyway that can reliably return the most recent date for the searched plate?</p>
  
  <pre><code>"9452995","268460266","1222584",\N,"-22.905745","-47.234562","2019-02-19 14:58:32","1","5.48 Km SSO de HORTOLANDIA, SP - Brasil, 81.00 Km/h","DPC-5345-Sami"
  "9452993","268460266","1223268",\N,"-5.873287","-35.220425","2019-02-19 14:58:06","1","6.59 Km NE de PARNAMIRIM, RN - Brasil, 62.00 Km/h","DCC-7567-"
  "9452992","268460266","1232171",\N,"-21.832002","-47.250372","2019-02-19 14:57:34","1","0.59 Km SSO de SANTA CRUZ DAS PALMEIRAS, SP - Brasil, 0.00 Km/h","IST-0719-RODO"
  "9452994","268460266","1229564",\N,"-20.262012","-42.166750","2019-02-19 14:57:17","1","13.90 Km O de MANHUACU, MG - Brasil, 88.00 Km/h","OHD-3670-SAHI"
  "9452990","268460266","1225289",\N,"-18.128898","-48.568003","2019-02-19 14:57:13","1","49.92 Km E de BURITI ALEGRE, GO - Brasil, 0.00 Km/h","GHK-9324-SIRA "
  "9452989","268460266","1220268",\N,"-23.625102","-48.097762","2019-02-19 14:57:01","1","5.91 Km SO de ITAPETININGA, SP - Brasil, 0.00 Km/h","DUR-6980-ROBESO"
  "9452988","268460266","1235318",\N,"-17.250372","-49.241770","2019-02-19 14:56:42","1","0.20 Km S de PROFESSOR JAMIL, GO - Brasil, 56.00 Km/h","DPD-7548-JOHE"
  "9452987","268460266","1222584",\N,"-22.908855","-47.213868","2019-02-19 14:56:32","1","5.66 Km S de HORTOLANDIA, SP - Brasil, 72.00 Km/h","DPC-5345-Sami"
  "9452986","268460266","1223268",\N,"-5.886993","-35.232365","2019-02-19 14:56:06","1","4.58 Km NE de PARNAMIRIM, RN - Brasil, 63.00 Km/h","DCC-7567-"
  "9452985","268460266","1232171",\N,"-21.831938","-47.250395","2019-02-19 14:55:34","1","0.59 Km SSO de SANTA CRUZ DAS PALMEIRAS, SP - Brasil, 0.00 Km/h","IST-0719-RODO"
  "9452983","268460266","1229564",\N,"-20.246693","-42.153427","2019-02-19 14:55:17","1","12.56 Km O de MANHUACU, MG - Brasil, 12.00 Km/h","OHD-3670-SAHI"
  "9452982","268460266","1225289",\N,"-18.128867","-48.567983","2019-02-19 14:55:13","1","49.92 Km E de BURITI ALEGRE, GO - Brasil, 0.00 Km/h","GHK-9324-SIRA "
  "9452981","268460266","1220268",\N,"-23.625102","-48.097708","2019-02-19 14:55:01","1","5.91 Km SO de ITAPETININGA, SP - Brasil, 0.00 Km/h","DUR-6980-ROBESO"
  "9452980","268460266","1235318",\N,"-17.233647","-49.234803","2019-02-19 14:54:42","1","1.82 Km NNE de PROFESSOR JAMIL, GO - Brasil, 88.00 Km/h","DPD-7548-JOHE"
  "9452979","268460266","1222584",\N,"-22.898880","-47.214478","2019-02-19 14:54:32","1","4.55 Km S de HORTOLANDIA, SP - Brasil, 81.00 Km/h","DPC-5345-Sami"
  "9452978","268460266","1223268",\N,"-5.890597","-35.250160","2019-02-19 14:54:06","1","3.07 Km NNE de PARNAMIRIM, RN - Brasil, 57.00 Km/h","DCC-7567-"
  "9452977","268460266","1232171",\N,"-21.831958","-47.250420","2019-02-19 14:53:34","1","0.59 Km SSO de SANTA CRUZ DAS PALMEIRAS, SP - Brasil, 0.00 Km/h","IST-0719-RODO"
  "9452968","268460266","1229564",\N,"-20.247340","-42.143567","2019-02-19 14:53:17","1","11.53 Km O de MANHUACU, MG - Brasil, 12.00 Km/h","OHD-3670-SAHI"
  "9452966","268460266","1225289",\N,"-18.128913","-48.567978","2019-02-19 14:53:13","1","49.92 Km E de BURITI ALEGRE, GO - Brasil, 0.00 Km/h","GHK-9324-SIRA "
  "9452965","268460266","1220268",\N,"-23.625155","-48.097717","2019-02-19 14:53:01","1","5.91 Km SO de ITAPETININGA, SP - Brasil, 0.00 Km/h","DUR-6980-ROBESO"
  "9452976","268460266","1235318",\N,"-17.212558","-49.224920","2019-02-19 14:52:42","1","4.39 Km NNE de PROFESSOR JAMIL, GO - Brasil, 75.00 Km/h","DPD-7548-JOHE"
  "9452964","268460266","1222584",\N,"-22.884487","-47.231652","2019-02-19 14:52:32","1","3.15 Km SSO de HORTOLANDIA, SP - Brasil, 73.00 Km/h","DPC-5345-Sami"
  "9452962","268460266","1223268",\N,"-5.890032","-35.264475","2019-02-19 14:52:06","1","2.82 Km N de PARNAMIRIM, RN - Brasil, 40.00 Km/h","DCC-7567-"
  "9452961","268460266","1232171",\N,"-21.831912","-47.250373","2019-02-19 14:51:34","1","0.58 Km SSO de SANTA CRUZ DAS PALMEIRAS, SP - Brasil, 0.00 Km/h","IST-0719-RODO"
  "9452959","268460266","1229564",\N,"-20.246538","-42.153748","2019-02-19 14:51:17","1","12.60 Km O de MANHUACU, MG - Brasil, 65.00 Km/h","OHD-3670-SAHI"
  "9452958","268460266","1225289",\N,"-18.128877","-48.568017","2019-02-19 14:51:13","1","49.92 Km E de BURITI ALEGRE, GO - Brasil, 0.00 Km/h","GHK-9324-SIRA "
  "9452963","268460266","1220268",\N,"-23.625228","-48.097698","2019-02-19 14:51:01","1","5.92 Km SO de ITAPETININGA, SP - Brasil, 0.00 Km/h","DUR-6980-ROBESO"
  "9452975","268460266","1235318",\N,"-17.197180","-49.208607","2019-02-19 14:50:42","1","6.71 Km NNE de PROFESSOR JAMIL, GO - Brasil, 86.00 Km/h","DPD-7548-JOHE"
  "9452957","268460266","1222584",\N,"-22.875095","-47.253175","2019-02-19 14:50:32","1","3.88 Km SO de HORTOLANDIA, SP - Brasil, 76.00 Km/h","DPC-5345-Sami"
  </code></pre>