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?

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • What is in $class? Where is the DB at all? those are just arrays... – Ron Dadon Nov 03 '15 at 15:13
  • `$resultclass = strtr($class , $classes, $replacements);` – Mark Baker Nov 03 '15 at 15:13
  • Not the best one, but if your classes will not become more, you can just switch the `1`and the `11`in your $classes array, so first it will replace 11 and later on 1 xD – swidmann Nov 03 '15 at 15:13
  • And read the [PHP Docs](http://php.net/manual/en/function.str-replace.php) 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. ` – Mark Baker Nov 03 '15 at 15:14
  • Take a look at this question and answer - you can use preg_replace: http://stackoverflow.com/questions/9416175/preg-replace-with-array-replacements – Ron Dadon Nov 03 '15 at 15:18

4 Answers4

5

Why not just do:

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

$resultclass = $replacements[$row["class"] - 1];
M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
  • Due that the class number and the values are not associated, I think this is a good solution; I'll upvote this – swidmann Nov 03 '15 at 15:19
  • 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. – M. Eriksson Nov 03 '15 at 15:24
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];
     ?>
Noob
  • 154
  • 1
  • 1
  • 14
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"

Laurentiu
  • 574
  • 6
  • 26
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];
Denis Brat
  • 487
  • 5
  • 14