jenkins Posted October 25, 2013 Share Posted October 25, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/283284-looping-through-array-compare-dates-if-duplicate-id/ Share on other sites More sharing options...
Psycho Posted October 25, 2013 Share Posted October 25, 2013 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/283284-looping-through-array-compare-dates-if-duplicate-id/#findComment-1455451 Share on other sites More sharing options...
jenkins Posted October 25, 2013 Author Share Posted October 25, 2013 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! Quote Link to comment https://forums.phpfreaks.com/topic/283284-looping-through-array-compare-dates-if-duplicate-id/#findComment-1455463 Share on other sites More sharing options...
Psycho Posted October 29, 2013 Share Posted October 29, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/283284-looping-through-array-compare-dates-if-duplicate-id/#findComment-1455961 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.