Jump to content

Combine array according to its values


NikkiLoveGod

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.