Array only taking first character of string

Discussion in 'PHP' started by skum, Feb 1, 2010.

  1. #1
    Need help with a small problem that I just don't understand.

    Example code (in a for-loop):
    
    switch($something)
    {
    	case 'Apple': $fruit="APP";break;
    	case 'Banana': $fruit="BAN";break;
    	case 'Orange': $fruit="ORA";break;
    }
    $fruitarray[$i]=$fruit;
    
    Code (markup):
    $fruitarray will then only contain the first letter (A,B or O) of the string. What am I missing?

    Thank you
     
    skum, Feb 1, 2010 IP
  2. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #2
    Try something like this.

    
    // make a dictionary instead of a switch
    $fruitkeys = array('Apple' => 'APP', 'Banana' => 'BAN', 'Orange' => 'ORA');
    
    // do whatever in for loop
    for (...) {
     $fruitarray[$i] = $fruitkeys[$something];
    }
    
    // debug
    print_r($fruitarray);
    
    PHP:
    Doing so, you don't have to do a switch, will shave off some microseconds.
     
    Kaizoku, Feb 1, 2010 IP
  3. skum

    skum Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you for the tip but it doesn't solve the problem.
     
    skum, Feb 1, 2010 IP
  4. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #4
    Can you show me your whole code?
     
    Kaizoku, Feb 1, 2010 IP
  5. skum

    skum Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Solved.

    I know it was something silly.
    I accidentaly uses the same variable for two different things.
     
    skum, Feb 1, 2010 IP