Need some help with this switch inside foreach loop?

I have script which takes data from mysql and inputs into a foreach loop. I then want to replace certain strings with another one depending on what the original string is. The data in mysql output in random order. It outputs everything the way I want except when it comes to time to replace certain strings with another one. I’ve search other similar threads and tried many of the suggestions, but didn’t get the results I’m looking for. Can somebody give me a hand with this please???

$data=(script retrieving info from mysql);

foreach($data[‘data’][‘data2’][‘data’] as $variable) {
$new_variable = $variable[‘data2’];
$new_variable = preg_replace(‘/\s*\s+/’, ‘’, $new_variable);

   switch($new_variable){
          case "apple":
	          $z = "red";
	        break;
                   case "bannanas":
	          $z = "yellow";
	          break;
         case "grapes":
	          $z="purple";
	          break;
                   case "oranges":
                                  $z="orange";
                                   break;
   }

   echo $z."br/>";

}

The script echos out $new_variable perfectly fine, but when I add the switch part, it doesn’t output anything or it outputs several lines of “red”.
Thanks for the help in advance.

try this:


switch(trim($new_variable))

It is most likely that your strings are returning not trimmed. Why are you doing this in PHP, anyway? Return the strings you want in the SQL instead. Can you post your SQL?