我正在寻找一种从数组中提取/获取值并将值分配给变量并获取所有值之和的方法。
更具体地说,用户将输入一组字母。如果存在匹配,将输入的字母将与现有数组的键进行比较。当数组中有匹配项时,键的对应值(具有匹配项)将分配给一个新变量,并计算值的总和(w/c 应为数字)。我一直在想办法解决这个问题,但由于经验不足,我还没有真正找到好的方法。
你们对此有什么想法吗?请问有什么建议吗?
以下是代码:
<?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);
?>
对于看过这些代码的人来说,是的,我已经发布了它,因为我似乎找不到一个错误。我能够修复错误,但现在的问题是我无法真正获得所需的输出。请理解。谢谢。