PHP:数组转换问题

This is my array:

$class = $row["class"];
 $classes = array( '1', '2', '4', '5', '6', '7', '8', '9', '10', '11'
    );
 $replacements = array( 'Warrior', 'Paladin', 'Hunter', 'Rogue',
   'Priest', 'Death Knight', 'Shaman', 'Mage', 'Warlock', 'Monk',
   'Druid' );
 $resultclass = str_replace( $classes, $replacements, $class );

My problem thou is that when i get the number 11 from the DB it displays "Warrior" twice and not "Druid"

How can i fix that?

Why not just do:

$replacements = array( 'Warrior', 'Paladin', 'Hunter', 'Rogue', 
   'Priest', 'Death Knight', 'Shaman', 'Mage', 'Warlock', 'Monk','Druid' );

$resultclass = $replacements[$row["class"] - 1];

Because "11" = "1"."1" all time will make replace for first match.

UPDATE

can use array_reverse:

    $class = "11";
 $classes = array_reverse(array( '1', '2', '4', '5', '6', '7', '8', '9', '10', '11'
    ));
 $replacements = array_reverse(array( 'Warrior', 'Paladin', 'Hunter', 'Rogue',
   'Priest', 'Death Knight', 'Shaman', 'Mage', 'Warlock', 'Monk',
   'Druid' ));
 $resultclass = str_replace( $classes, $replacements, $class );

var_dump($resultclass);

Now first match will start from big number to minimum, but be carefull if you have 12 that will take your 12th element not "1"."2"

Why not do it like this, store it too a array and load it out of an array.

<?PHP
    $class = $row["class"];
    $classes = array( 
    '0' => 'Warrior' , '1' => 'Paladin' , '2' => 'Hunter' , '3' => 'Rogue', 
    '4' => 'Priest', '5' => 'Death Knight', '6' => 'Shaman',
    '7' => 'Mage', '8' => 'Warlock', '9' => 'Monk' ,'10' => 'Druid' ,
    );

     $resultclass = $classes[$class];
     ?>

The way str_replace works is by iterating your $search array (first parameter) and replacing each found value by the value found at the same index in the $replace array (second argument). The precedence of the replacement are in the order the arrays are defined.

Now, when the function steps on your first search string '1', it finds two corresponding values at your string '11'. That's why you should be getting 'WarriorWarrior' instead of 'Druid'.

You can make a 'quick' fix for that behavior by reverting the order of the arrays:

$classes = array_reverse($classes);
$replacements = array_reverse($replacements);

But if you want one-to-one replacement, there is not really a need for str_replace. You can do something like this instead:

$class_index = array_search($class, $classes);
$resulting_class = $replacements[$class_index];