fesan Posted April 13, 2015 Share Posted April 13, 2015 Hi, is there possible to search and "choose" a certain arrey key by user input? And when the input is not "korrect" how to choose the closest one? Input = 22 $kz should chose key 21.3 => 1.17 $kz = array( 4.6 => 0.85, 6.1 => 0.90, 7.6 => 0.94, 9.1 => 0.98, 12.2 => 1.04, 15.2 => 1.09, 18 => 1.13, 21.3 => 1.17, 24.4 => 1.21, 27.4 => 1.24, 30.5 => 1.26, 36.6 => 1.31, 42.7 => 1.36, 48.8 => 1.39, 54.9 => 1.43, 61.0 => 1.46, 76.2 => 1.53, 91.4 => 1.59, 106.7 => 1.64, 121.9 => 1.69, 137.2 => 1.73, 152.4 => 1.77,); To explain some more, the array is part of a math solving. where the key is a height in meters and the value are the responding coefficient. $qz = 0.613 * $kz * 1 * 1 * pow($vind2, 2) *1; Link to comment https://forums.phpfreaks.com/topic/295522-search-to-specific-array-key/ Share on other sites More sharing options...
CroNiX Posted April 13, 2015 Share Posted April 13, 2015 I don't believe you can the way you have it. You can't use decimal numbers as array keys. If I copy/paste your $kz array and just print_r($kz), you get back whole numbers for the array indices instead of your decimals (24.4 becomes just 24). If your keys were strings containing a decimal, then it could be workable. Link to comment https://forums.phpfreaks.com/topic/295522-search-to-specific-array-key/#findComment-1508964 Share on other sites More sharing options...
requinix Posted April 13, 2015 Share Posted April 13, 2015 Make sure $kz is sorted, then foreach until the previous key = desired key. If it's not an exact match then pick whichever of the previous and current keys is closest. Link to comment https://forums.phpfreaks.com/topic/295522-search-to-specific-array-key/#findComment-1508966 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.