user1713438
user1713438

Reputation: 57

Correct Way to Add Values from a PHP Switch Statement?

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!

Upvotes: 0

Views: 1355

Answers (2)

Carlos Robles
Carlos Robles

Reputation: 10947

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;
   }

Upvotes: 0

Dave
Dave

Reputation: 3658

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!

Upvotes: 1

Related Questions