5

I'm trying to write a script that shows every value in array keys using switch statement here's my simple code:

<?php
$char = array('A'=>'01', 'B'=>'02', 'C'=>'03', 'D'=>null);

foreach($char as $letter => $number) 
{
    switch($char[$letter]) 
    {
        case 'A':
            echo $number;
            break;
        case 'B':
            echo $number;
            break;
        case 'C':
            echo $number;
            break;
        case 'D':
            echo $number;
            break;
        default:
            echo 'LETTER '.$letter.' is empty';
    }
}
?>

PROBLEM:

It won't print the values that has been stored in the array.

EXPECTED OUTPUT:

if A exist return 01 . . . . . . . and so on. But if the array key contains an empty value it returns 'LETTER D is empty'

any help please? thank you

5 Answers 5

12

Use switch($letter) instead of switch($char[$letter]). PHP foreach loop splitted your array as $letter=>A and $number=>01

    $char = array('A'=>'01', 'B'=>'02', 'C'=>'03', 'D'=>null);

    foreach($char as $letter => $number) 
    {
        switch($letter) 
        {
            case 'A':
                echo $number;
                break;
            case 'B':
                echo $number;
                break;
            case 'C':
                echo $number;
                break;
            case 'D':
                if($number=='' || is_null($number)){ 
                  echo 'LETTER '.$letter.' is empty';
                }else{
                  echo $number;
                }
                break;
            default:
                echo 'LETTER '.$letter.' is empty';
        }
    }

OR

    $char = array('A'=>'01', 'B'=>'02', 'C'=>'03', 'D'=>null);

    foreach($char as $letter => $number) 
    {          
        CheckNumber($letter, $number);                
    }


    function CheckNumber($letter, $number){         
        if($number=='' || is_null($number)){    // add whatever condition you want to check     
            echo 'LETTER '.$letter.' is empty';             
        }else{              
            echo $number;
        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

it won't return 'LETTER D is empty.
combine case D with the default: \n case 'D': \n default: \n echo 'LETTER '.$letter.' is empty';
@Destiny Updated answer, Can you please check
0

Replace

switch($char[$letter]) as switch($letter)

Comments

0

You are using the value inside that array index, not the index. Try this.

switch($letter) 
    {
        case 'A':
            echo $number;
            break;
        case 'B':
            echo $number;
            break;
        case 'C':
            echo $number;
            break;
        case 'D':
            echo $number;
            break;
        default:
            echo 'LETTER '.$letter.' is empty';
    }

Comments

0

Try This: It will give the answer :LETTER D is empty (Default only execute only when no condition is satisfied , if you want to output "letter D is empty" then you have to wrie code inside the letter match case. Here , case D match but there is only code to echo number and there is no number assign for D in array , so t shows nothing. )

$char = array('A'=>'01', 'B'=>'02', 'C'=>'03', 'D'=>null);

foreach($char as $letter => $number) 
{
    switch($letter) 
    {
        case 'A':
           if($number==null)
           {
            echo "LETTER ".$letter."is empty";
            }
           else
           {
           echo $number;
            }
            break;
        case 'B':
             if($number==null)
           {
            echo "LETTER ".$letter."is empty";
            }
           else
           {
           echo $number;
            }
            break;
        case 'C':
             if($number==null)
           {
            echo "LETTER ".$letter."is empty";
            }
           else
           {
           echo $number;
            }
            break;
        case 'D':
            if($number==null)
           {
            echo "LETTER ".$letter."is empty";
            }
           else
           {
           echo $number;
            }
            break;
        default:
            echo 'LETTER '.$letter.' is empty';
    }
}

1 Comment

that's a chain :( i don't like my script to be so long
0
       /// This is what I did ... 

         $i = (isset($somevalue))? $somevalue : NULL;  /// where $somevalue could be GET or POST ..or whatever
         $x = array(NULL=>'','monkey'=>'brown','baseball'=>'round','key3'=>'value3');

         $x['brisket'] = 'good'; // if you wanted to add another value...
         $x['pickles'] = 'notSoGood'; // if you wanted to add another value...

        /// uncomment below to view your array values
         // echo '<pre>';
         // var_dump($x); 
         // echo '<pre>';

            switch($i){
                case $i: 
                echo 'my value'.$x[$p];  
                //include_once $x[$p];    
                break;

                default: 
                echo 'default value';   
                //include_once $somedefault;
          }

Comments

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.