Jump to content

help with arrays again


Jaret

Recommended Posts

I have an array containing a few other arrays:

$s_pos_l = array( 
'1' => array( '0' => '2', 'x' => '2' , '1' => '6' , 'y' => '6' ),
'2' => array( '0' => '2', 'x' => '2' , '1' => '7' , 'y' => '7' ),
'3' => array( '0' => '2', 'x' => '2' , '1' => '5' , 'y' => '5' ),
'4' => array( '0' => '2', 'x' => '2' , '1' => '8' , 'y' => '8' ),
'5' => array( '0' => '1', 'x' => '1' , '1' => '4' , 'y' => '4' ),
'6' => array( '0' => '1', 'x' => '1' , '1' => '9' , 'y' => '9' )
); 

 

Then I have:

$tx = 3;
$ty = 10;

 

I want to get X and Y from one of the arrays, where X and Y are closest to TX and TY (but are from the same array).

 

Link to comment
https://forums.phpfreaks.com/topic/185595-help-with-arrays-again/
Share on other sites

I take no credit for this one, I just happened upon it recently as I was looking for similar.

 

$crit=10;
sort($array);
for($i=0;$i<count($array);$i++) {
if($crit>$array[$i]) {
if(($array[$i]-$array)>($array-$array[$i-1])) {
$closest=$array[$i-1];
} else {
$closest=$array[$i];
}
}
}
echo($closest);

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.