Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8133665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:41:12+00:00 2026-06-06T09:41:12+00:00

I am attempting to find the closest product to the given budget $array =

  • 0

I am attempting to find the closest product to the given budget

$array = array(
    'productname1' => 5,
    'productname2' => 10,
    'productname3' => 15
)

$budget = 12;

I have tried using a function like the following to find the nearest value, but it only returns the number which is closest to the budget rather than the product name.

   function closest($array, $number) {
        sort($array);
        foreach ($array as $a) {
            if ($a >= $number) return $a;
        }
        return end($array);
    }

I can’t help but think there is a MUCH better implementation of this. Any help would be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:41:13+00:00Added an answer on June 6, 2026 at 9:41 am
    foreach($array as $k => $v){ $diff[abs($v - $budget)] = $k; }
    ksort($diff, SORT_NUMERIC);
    $closest_key = current($diff);
    
    var_dump($closest_key);         // Product Name
    var_dump($array[$closest_key]); // Product Cost
    

    Prints:

        string(12) "productname2"
        int(10)
    

    Or as a function:

    function closest($array, $price)
    {
        foreach($array as $k => $v){ $diff[abs($v - $price)] = $k; }
        ksort($diff, SORT_NUMERIC);
        $closest_key = current($diff);
        return array($closest_key, $array[$closest_key]);
    }
    
    print_r(closest($array, $budget));
    

    Prints:

        Array
        (
            [0] => productname2  // Product Name
            [1] => 10            // Product Price
        )
    

    Both formats include only three steps:

    • Calculate the difference between the product cost and the budget
    • Sort these
    • Take the first element from the sorted array (the element whose price is closest to the budget).

    EDIT: If you don’t care about anything other than the single closest product, then a sort is overkill and a simple min() function (like Emil used) would be a lot faster. For example:

    function closest($array, $price)
    {
        foreach($array as $k => $v){ $diff[abs($v - $price)] = $k; }
        $closest_key = $diff[min(array_keys($diff))];
        return array($closest_key, $array[$closest_key]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For the last few days I have been attempting to find a method to
Bah! I have recently installed the MiniProfiler from http://code.google.com/p/mvc-mini-profiler/ attempting to find all the
I am attempting to find a binary file in a Linux system using something
I have the following code inside a Form, and am attempting to find a
Here is my code, I have been attempting to find a way to do
Attempting to find printers / shares in Active Directory using C#. This is my
I am attempting to find 'entries' create today (in my current timezone) using the
I am attempting to find the MAC address using pcap for a small project.
I am attempting to find all entries in the 'archive' table that have the
I'm attempting to find duplicate instances of strings where I have a vector of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.