0

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?

5
  • What is in $class? Where is the DB at all? those are just arrays... Commented Nov 3, 2015 at 15:13
  • $resultclass = strtr($class , $classes, $replacements); Commented Nov 3, 2015 at 15:13
  • Not the best one, but if your classes will not become more, you can just switch the 1and the 11in your $classes array, so first it will replace 11 and later on 1 xD Commented Nov 3, 2015 at 15:13
  • And read the PHP Docs it has a big warning about str_replace Replacement order gotcha Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements. See also the examples in this document. Commented Nov 3, 2015 at 15:14
  • Take a look at this question and answer - you can use preg_replace: stackoverflow.com/questions/9416175/… Commented Nov 3, 2015 at 15:18

4 Answers 4

5

Why not just do:

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

$resultclass = $replacements[$row["class"] - 1];
Sign up to request clarification or add additional context in comments.

2 Comments

Due that the class number and the values are not associated, I think this is a good solution; I'll upvote this
Yepp. Looking at the question, it seems like he just wants to "translate" a number to a name and this would do just that. Using preg_replace or anything else seems over the top.
1

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];
     ?>

Comments

0

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"

Comments

0

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

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.