Step rate solution in the project, find the key value in the array by key name

Source: Internet
Author: User

<?php
$a = 3.5;
$arr = Array ("1" =>4, "2.5" =>5, "5" =>6);//decimal key name, need to quote
$arr = Array_flip ($arr);

foreach ($arr as $key = = $value) {
$b [] = $value;
}

The main idea is to flip the array, take out the original array of all the key names to form a one-dimensional array, determine in which ladder range, and then reverse the array to array_search out the corresponding key value, this key value is the ladder corresponding to the rate value

for ($i =0; $i <=count ($b); $i + +) {
if ($a > $b [$i] && $a < $b [$i +1]) {
$c = $b [$i +1];
}elseif ($a = = $b [$i]) {
$c = $b [$i];
}elseif ($a >= max ($b)) {
$c = max ($b);
}elseif ($a <= min ($b)) {
$c = min ($b);
}
}

Echo (Array_search ($c, $arr));

Step rate solution in the project, find the key value in the array by key name

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.