find closest value in array

    Date: 03/23/07 (PHP Community)    Keywords: no keywords

    I have an array of sizes:
    $standardSizes = array("60x60", "90x60", "120x60", "120x90", "120x120", "150x120", "180x120", "240x120");

    User is allowed to choose the size of the board they want from these standard sizes, or pick a custom size up to 240x120. The price of a custom board is worked out at the size of the next board up - i.e. if User wants an 80x50 board, they'd be charged for a 90x60.

    So, how do I work out what is the next size up? The code I have at the moment is:

    if (!isset($_COOKIE['magi']['size'])) echo $warning; else $dims = explode("x", $_COOKIE['magi']['size']);
    
    if (!in_array($_COOKIE['magi']['size'], $standardSizes)) {
    	foreach($standardSizes as $size) {
    		$size = explode("x", $size);
    		if ($dims[0] <= $size[0]) $compWidth = $size[0];
    		if ($dims[1] <= $size[1]) $compHeight = $size[1];
    	}
    } else {
    	$compWidth = $dims[0];
    	$compHeight = $dims[1];
    }
    echo $compWidth ." x ". $compHeight;
    


    ($dims[0] = custom width, $dims[1] = custom height .. $compWidth needs to be the closest standard width, $compHeight the closest standard height)

    However, this (logically) only gives me the size of the biggest board. Any ideas how I should approach this? I'm stuck.

    Edit: Solved by '[info]'shippo, thank you :)

    Source: http://community.livejournal.com/php/555598.html

« Case of the dumb || Totally unrelated; but... »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home