I am looking for a way to extract/get values from an array and assign the values to a variable and get the sum of all values.
To be more specific, the user will be entering a certain group of letters. The letters that will be entered will be compared to the keys of an existing array if there are matches or not. When there are matches in the array, the corresponding values of the keys(that have matches) will be assigned to a new variable, and the sum of the values (w/c are expected to be numeric) will be calculated. I have been trying to find a way for this, but due to inexperience, I have not really found a good way.
Do you guys have any ideas about this? Suggestions please?
Here are the codes:
<?php
class GreekNum
{
public function setText($text)
{
$this->text=$text;
}
public function getText()
{
return $this->text;
}
public function Convert($text)
{
$TextLength = strlen($text);
$text = strtoupper($text);
$text = str_split($text);
$collection = array(
"A" => 1,
"B" => 2,
"G" => 3,
"D" => 4,
"E" => 5,
"#" => 6,
"Z" => 7,
"Y" => 8,
"H" => 9,
"I" => 10,
"K" => 20,
"4" => 30,
"M" => 40,
"N" => 50,
"X" => 60,
"O" => 70,
"P" => 80,
"Q" => 90,
"R" => 100,
"S" => 200,
"T" => 300,
"U" => 400,
"F" => 500,
"C" => 600,
"$" => 700,
"W" => 800,
"3" => 900,
);
if(isset($text))
{
$total = 0;
for($i=0; $i<$TextLength; $i++)
{
if(array_key_exists($text[$i], $collection))
{
$total += $text[$i];
}
}
return $total;
}
}
}
$GreekNum = new GreekNum();
$text = $_POST['text'];
$GreekNum->setText($text);
echo "<br>";
echo "<b>Entered Values: </b><br>". $GreekNum->getText();
echo "<br>";
echo "<br>";
echo "<b>Decimal Number Equivalence: </b><br/>". $GreekNum->Convert($text);
?>
To the guys who have seen these codes, yes, I have posted it because there was an error that I cant seem to find. I was able to fix the error but the issue now is that I can't really get the output that I need. Please do understand. Thanks.