NikkiLoveGod Posted April 18, 2010 Share Posted April 18, 2010 Hi all, Im having a bit of trouble figuring out this logic, and its getting rather annoying, to be honest. This should be simple, but maybe i've been just too stuck on this. I need to combine my array, according to its values. I have this array: $compareArray = array( array('start' => 90, 'end' => 100), array('start' => 80, 'end' => 95), array('start' => 70, 'end' => 75) ); And this is what I want to get: $destArray = array( array('start' => 70, 'end' => 100) ); So basicly, i want to go through the array, check if the arrays start or date values are with in the range of 5, if so, combine them into one row. This is where I am at the moment: $buffer = 5; foreach($compareArray as $needleRow => $needle) { foreach($compareArray as $haystackRow => $haystack) { if($needle['start'] < $haystack['start'] && $needle['end'] >= ($haystack['start'] - $buffer)) { $start = $needle['start']; } else { $start = $haystack['start']; } if($needle['end'] > $haystack['end'] && $needle['start'] <= ($haystack['end'] + $buffer)) { $end = $needle['end']; } else { $end = $haystack['end']; } /* SOMETHING TO UNSET THE COMBINED ROW */ } } And im at a mental block, and can't get ahead of this for some reason. The IF -check should be valid, there shouldn't be any problem, but its the logic behind combining the arrays, that I cant figure. These values are actually a range of dates, but for the simplicity, I changed them to just numbers. So, any help here? Thanks alot! EDIT: To clarify the if -checks, it should check if the numbers are like "75 - 95" and "60 - 70", combine them to "60 - 95", or if the numbers are "50 - 90" and "60 - 80" it combines them to "50 - 90", "55 - 70" & "60 - 80" = "55 - 80" "55 - 70" & "40 - 65" = "40 - 70" and so on. BUT if the values are "50 - 70" and "80 - 90" it should leave them separated, because they are not with in the range of 5 of them selves. Link to comment https://forums.phpfreaks.com/topic/198949-combine-array-according-to-its-values/ Share on other sites More sharing options...
NikkiLoveGod Posted April 19, 2010 Author Share Posted April 19, 2010 Do you guys have any salvation for me on this one, or is this actually a hard question, and not just my own dark moment? I've been stuck with this for ages Link to comment https://forums.phpfreaks.com/topic/198949-combine-array-according-to-its-values/#findComment-1044382 Share on other sites More sharing options...
NikkiLoveGod Posted April 19, 2010 Author Share Posted April 19, 2010 Managed to get it working, problem lies with in the foreach statements way of handling the variable, as it is not a reference, but a copy. (http://fi.php.net/manual/en/control-structures.foreach.php) This is the solution $buffer = 5; foreach($compareArray as $needleRow => $needle) { //skip the unsetted rows if(in_array($needleRow, $unset)) { continue; } $start = $needle['start']; $end = $needle['end']; $changes = false; foreach($compareArray as $haystackRow => $haystack) { $needle = $compareArray[$needleRow]; if($haystackRow == $needleRow) { continue; } echo 'comparing ' . $needleRow . ' -> ' . $haystackRow . "\r\n"; if($haystack['start'] < $start && $haystack['end'] >= ($start - $buffer)) { $start = $haystack['start']; $changes = true; } if($haystack['end'] > $end && $haystack['start'] <= ($end + $buffer)) { $end = $haystack['end']; $changes = true; } if($changes == true) { //Update the correct values $compareArray[$needleRow]['start'] = $start; $compareArray[$needleRow]['end'] = $end; //Unset the combined row unset($compareArray[$haystackRow]); //And store the row, so we can actually skip it with in the foreach $unset[] = $haystackRow; } Link to comment https://forums.phpfreaks.com/topic/198949-combine-array-according-to-its-values/#findComment-1044470 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.