Jump to content

looping through array, compare dates if duplicate ID


jenkins

Recommended Posts

Hi,

I have the following array and would like to loop through it and check if there are duplicate catalogIDs, if yes, I would like to compare the dates and choose the earliest date.

 

In the following array:

catalogID 1257 is there two times. I need earliest date, which is 2013-09-03

catalogID 1340 is there three times. I need earliest date, which is 2013-09-04

Array
(
   [0] => 1159, 2013-09-03
   [1] => 1157, 2013-09-03
   [2] => 1257, 2013-09-03
   [3] => 1257, 2013-09-13
   [4] => 1340, 2013-09-04
   [5] => 1335, 2013-09-03
   [6] => 1340, 2013-09-13
   [7] => 1410, 2013-09-03
   [8] => 1340, 2013-09-18
)

Is it easier to add all the unique catalogIDs with the earliest date to a new array or remove the duplicate catalogIDs with the late dates from the current array? 

 

I have tried a foreach loop and a for loop with counter but I'm not able to do this.

 

Any ideas?

 

Thanks!

 

Where is this data coming from? This would be a much simpler problem if the data in the array was structured differently. So, if you have the ability to restructure the data - that would be the better solution. But, with what you have, this would work

 

<?php

$records = array(
   '0' => '1159, 2013-09-03',
   '1' => '1157, 2013-09-03',
   '2' => '1257, 2013-09-03',
   '3' => '1257, 2013-09-13',
   '4' => '1340, 2013-09-04',
   '5' => '1335, 2013-09-03',
   '6' => '1340, 2013-09-13',
   '7' => '1410, 2013-09-03',
   '8' => '1340, 2013-09-18'
);

$final = array();
foreach($records as $record)
{
    list($catalogID, $date) = explode(', ', $record);
    //Check if a record exists for this catalog ID
    if(isset($final[$catalogID]))
    {
        //Check if this date is earlier
        if($date < $final[$catalogID])
        {
            //This date is earlier - change it
            $final[$catalogID] = $date;
        }
    }
    else
    {
        //Record hasn't exist yet - add it with this date
        $final[$catalogID] = $date;
    }
}

echo "<pre>" . print_r($final, 1) . "</pre>";

?>

 

Output:

 

Array
(
    [1159] => 2013-09-03
    [1157] => 2013-09-03
    [1257] => 2013-09-03
    [1340] => 2013-09-04
    [1335] => 2013-09-03
    [1410] => 2013-09-03
)

Hi Psycho,

Thanks for your reply.

Your solution works well, but I don't think I would have ever solved it like this :)

 

I can get the data as follows from a xml file. When I tried directly from the xml file I kept getting an error when parsing.

 

Would this be easier: 

 

foreach ($result as $catID) { 
     $catalogID = $catID->CAT_ID;  
     $date = $catID->CAT_DATE;
}
 
Thanks again!
 

Would this be easier: 

 

foreach ($result as $catID) { 
     $catalogID = $catID->CAT_ID;  
     $date = $catID->CAT_DATE;
}

 

That would only work if you are 100% certain that the last entry for any duplicate dates contains the value you want. Since that wasn't stated as a known prerequisite I coded it to always ensure the last date would be the one set for each id.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.