0

I am looking to take the next step on my switch statement. I need to keep both pieces of information generated from the case statement (knowing whether the letter is a vowel and knowing it's numeric value). I then need to add the values generated by the below function but can't figure out through research or tutorials how to do this. I have made a string that looks like an array, but I don't think that is the best way.

    $firstName = strtoupper("Abc");
    $firstNamesArray = str_split($firstName);
    $string = "(";

    foreach ($firstNamesArray as $value) {
        $newValue = (getLetter($value)) . " ";

        $string .=$newValue;

        }
     echo "<br>";
     $string .=")";
     echo $string;

     function getLetter($letter) {
         switch ($letter) :
         case  "A": 
             return '"V" => 1'; break;
         case  "B": 
             return '"C" => 2'; break;
         case  "C": 
             return '"C" => 3'; break;
         default: 
             return 'This is not a valid selection';
         endswitch;
         } 

I want to add the values 1 + 2 + 3 (the second part of the case return value).

I appreciate your advice/assistance!

1
  • Can you provide an example of exactly the result you're looking to produce? How exactly do you expect to add "v => 1" and "c => 2"? Commented Dec 12, 2013 at 21:24

2 Answers 2

1

Modify your getLetter() function so that it returns an array:

function getLetter($letter) {
    switch ($letter) :
        case  "A": 
            return array('v', 1);
            break;
        case  "B": 
            return array('c', 2);
            break;
        case  "C": 
            return array('c', 3);
            break;
        default: 
            return false;
    endswitch;
}
$firstName = strtoupper("Abcd");
$firstNamesArray = str_split($firstName);
$letters = '';
$numbers = 0;
foreach ($firstNamesArray as $value) {
    $data = getLetter($value);
    if ( is_array($data) ) {
        /**
         *  Append the first value of the array to $letters
         */
        if ( isset($data[0]) ) {
            $letters .= $data[0];
        }
        /**
         *  Add the second value of the array to $numbers
         */
        if ( isset($data[1]) ) {
            $numbers += $data[1];
        }
    } else {
        /**
         *  Nothing found in switch, so returned false
         */ 
    }

}

$letters is now "vcc" and $numbers is "6". Those are examples.

Note that you could make a small change and, from within the switch, return an array with a key and value. Or you can return a multi-dimensional array. Decide what works best for you!

Sign up to request clarification or add additional context in comments.

1 Comment

I did indeed return values that should help me take the next step. Thank you, Dave!
0

Your approach is not really clean, and you should avoid something like that since it will be very hard to deal with.

I would use one of two possible approach, depending of the use you are going to do with it, or which one you feel more confrotable with.

Approach 1. Using a PHP object

You define a class, and the in every case, you create an object and put it into the array

class Letter
{

    public $value;
    public $type;


    public  __construct($type, $value) {
        $this->type=$type;
        $this->value=$value;
    }
}

 $firstName = strtoupper("Abc");
$firstNamesArray = str_split($firstName);

$result=array();

 foreach ($firstNamesArray as $value) {
 $result[]=  getLetter($value);

    }
 print_r($result);

 $sum=0;
 foreach ($result as $r)
      if (!is_null($r))
           $sum+= $r->value;

 //here, in sum you will have the result of 1+2+3

 function getLetter($letter) {


     switch ( $letter)  :
     case  "A": 
         return new Letter("V",1);break;
    case  "B": 
         return new Letter("C",2); break;
     case  "C": 
         return new Letter("C",2); break;
     default: 
         return null;
     endswitch;
   }

Approach 2. Keeping 2 Arrays

one for a boolean indicating if its vocal or consonant, and one for the numeric value. This approach is very straightforward so i wont provide example :)

 $firstName = strtoupper("Abc");
$firstNamesArray = str_split($firstName);

$types=array();
$values=array();

 foreach ($firstNamesArray as $value) {
     getLetter($value);

 }

print_r($values);
 print_r($types);

 $sum=0;
 foreach ($values as $r)

           $sum+= $r ;

  echo $sum;
 //here, in sum you will have the result of 1+2+3

 function getLetter($letter) {
     global $types,$values;
     switch ( $letter ) :
     case  "A": 
       $types[]="V";
       $values[]=1;
        break;
    case  "B": 
        $types[]="C";
       $values[]=2;
        break;
     case  "C": 
        $types[]="C";
       $values[]=3;
        break;
     default: 
        break;
     endswitch;
   }

7 Comments

Carlos, I am getting an error on this line: public function__construct($type, $value) saying: syntax error, unexpected T_STRING, expecting T_VARIABLE. I'm afraid I'm new to OOP and don't see the issue. Would you be so kind as to provide that straightforward example for a newbie?
you got it! anyways, that i have a typo in that function, sorry, i didn't know you was newbie! you can try again if you want or use the second approach
I definitely am a newbie. When I try the second way and add an echo $sum; to see the result, I get 0. How are the case values getting returned from the function? Yet, when I try adding the text 'return', I get an error.
yeah, actually those are really newbie questions. you should read a few more tutorials and write a lot of code and you will be happier :) The function is not returning anything, it is filling the arrays, and that is enough. probably you can want to change it before to uppercase, if the function shouldnt be case sensitive. I will edit it again. I cannot see any other problem you should have.
if you do print_r($values); print_r($types); what do you see?
|

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.