#### Archived

This topic is now archived and is closed to further replies.

# Array search or alternative

## Recommended Posts

I am writing a function to convert a table of numbers from decimal e.g. (4.25) to fractional form (4 1/2) Certain columns of the table need to be ignored because they contain a letter or there is no fractional equivalent.  The fractional form is to a 1/16 accuracy.  (You can see a sample table here:  [url=http://www.piping-designer.com/pages/flanges/fl150wnrtj.php]http://www.piping-designer.com/pages/flanges/fl150wnrtj.php[/url])

I wrote the following function. My problem is this, when I search for items that are not listed in the array, it returns just the integer and that is unacceptable for this project.  I have tried creating a fraction with 16ths as a denominator and reducing the fraction using the Euclidian algorithm  but I reached same problem when the fraction would not reduce. How can I get the function to exit if \$key is not found in the array?

Here's the code:
[code]<?php
function fraction(\$value){ // Define arrray that we are checking against

\$values = array('1/16' => .0625, '1/8' => .125, '3/16' => .1875,
'1/4' => 0.25, '5/16' => .3125, '3/8' => .375, '7/16' => .4375,
'1/2' => .5, '9/16' => .5625, '5/8' => .625, '11/16' => .6875,
'3/4' => .75, '13/16' => .8125, '7/8' => .875, '15/16'=>.9375);

if (eregi('[[:alpha:]]', \$value)) {
return \$value;                                        //contains any letter letter we're done here
}else{                                                                        //There are no letters so we continue
\$intnum=floor(\$value);                                 //take the value and round it down
\$dec = \$value - \$intnum;                                        //Get the decimal portion of the original value

\$key = array_search(\$dec, \$values,strict); //search the array for the decimal value

if (\$intnum < 1){                                                        //if the value is less than zero, then we will return just a fraction
\$value = \$key;
}else{                                                                     //otherwise we will return the integer and the fraction
\$value = \$intnum." ".\$key;
}
return \$value;

}
}
?>[/code]

EDIT:  Tried to fix formatting but failed horribly

##### Share on other sites
it return "just the integer" because this line
[code]\$value = \$intnum." ".\$key;[/code]
gives just rounded up value (array_search() returns FALSE, so \$key is empty)

You can check if array_seach() returns FALSE and return if it does ( return the original value, or what ever you want)

##### Share on other sites
Thanks a million!  I put this in:  if (\$key){  and it worked like a charm!

##### Share on other sites
try
[code]
<?php

function nearestFraction (\$input) {
\$values = array('' => 0.0, '1/16' => .0625, '1/8' => .125, '3/16' => .1875,
'1/4' => 0.25, '5/16' => .3125, '3/8' => .375, '7/16' => .4375,
'1/2' => .5, '9/16' => .5625, '5/8' => .625, '11/16' => .6875,
'3/4' => .75, '13/16' => .8125, '7/8' => .875, '15/16'=>.9375, '1'=> 1.0);

\$int = (int)floor(\$input);
\$dec_in = \$input - \$int;

\$min = 1.0;
\$nearfrac = '';
foreach  (\$values as \$frac => \$dec) {
if (abs(\$dec - \$dec_in) < \$min) {
\$min = abs(\$dec - \$dec_in);
\$nearfrac = \$frac;
}
}
return (\$nearfrac == '1') ? \$int++ : "\$int \$nearfrac";
}

echo nearestFraction(5.905);
?>
[/code]