Jaret Posted December 18, 2009 Share Posted December 18, 2009 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 More sharing options...
monkeytooth Posted December 18, 2009 Share Posted December 18, 2009 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); Link to comment https://forums.phpfreaks.com/topic/185595-help-with-arrays-again/#findComment-979908 Share on other sites More sharing options...
Jaret Posted December 18, 2009 Author Share Posted December 18, 2009 It looks for only one value, and also I can't get it to work. Link to comment https://forums.phpfreaks.com/topic/185595-help-with-arrays-again/#findComment-979916 Share on other sites More sharing options...
emopoops Posted December 18, 2009 Share Posted December 18, 2009 your welcome. just use keys and u will be fine. u know $aho1 => $aho2 its how it goes. not using keys is a big deficiet and im not willing to take the chance in not letting u knwo. i meant that in reply to monkuy. that guy has no freakin keys Link to comment https://forums.phpfreaks.com/topic/185595-help-with-arrays-again/#findComment-979937 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.