1

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.

4
  • 2
    Let's see the code you have thus far . . . the logic you've outlined above sounds reasonable enough . . . Commented Jul 24, 2012 at 0:37
  • @ernie: I have just edited this post and added the codes. So what do you think about what I was able to come up? Commented Jul 24, 2012 at 0:47
  • How is this different from your other question? If it's different, please accept an answer on the other one, and then explain how the output you're getting is not what you need, e.g. show us the output you're getting, and the output you'd like to be getting. Commented Jul 24, 2012 at 0:51
  • @ernie: The question was actually about an error that I was not able to solve. Some guys answered my question and I was able to fix the error. Now going to the new problem that arose, on the code that I have provided above, the final output which is the last line of the code is always 0. What I actually would like to get is the sum of the values of the keys that have matches with the letters entered by the user. :) Commented Jul 24, 2012 at 0:57

1 Answer 1

1

If you are seeking to get the sum of the values for each letter entered, then replace this :

$total += $text[$i];

By this :

$total += $collection[$text[$i]];
Sign up to request clarification or add additional context in comments.

1 Comment

Glad it helped you. :) Could you mark this answer as "accepted" (by clicking the green checkbox), so that it gets marked as resolved ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.