# Array search or alternative

3 replies to this topic

### #1 EngineeringGuy

EngineeringGuy
• Members
• Member
• 12 posts
• LocationBakersfield, CA - 6 feet from Hell

Posted 22 October 2006 - 07:27 PM

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:  http://www.piping-de.../fl150wnrtj.php)

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:
```<?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;

}
}
?>```

EDIT:  Tried to fix formatting but failed horribly

### #2 LazyJones

LazyJones
• Members
• 78 posts

Posted 22 October 2006 - 07:51 PM

it return "just the integer" because this line
`\$value = \$intnum." ".\$key;`
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)

### #3 EngineeringGuy

EngineeringGuy
• Members
• Member
• 12 posts
• LocationBakersfield, CA - 6 feet from Hell

Posted 22 October 2006 - 08:01 PM

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

### #4 Barand

Barand
• Moderators
• Sen . ( ile || sei )
• 18,023 posts

Posted 22 October 2006 - 08:35 PM

try
```<?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);
?>
```

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.