Jump to content

Replacing numbers


NLCJ

Recommended Posts

Hello,

I've got multiple variables that are all an array. I set them up like:

$round1 = ...;
$round2 = ...;
$round3 = ...;

All the way up to 10, now I created this:

$numbers = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
$rounds = array("round1", "round2", "round3", "round4", "round5", "round6", "round7", "round8", "round9", "round10");
for($i = 1; $i < 11; $i++) {
$newi = str_replace($numbers, $rounds, $i);
echo $newi."<br />";
}

Sadly this is only working partially, the output is:

round1
round2
round3
round4
round5
round6
round7
round8
round9
roundround10

 

Does anybody know why? If I make it "$i = 0;" the result is:

roundroundroundroundroundroundroundroundroundround10
roundroundroundroundroundroundroundroundround10
roundroundroundroundroundroundroundround10
roundroundroundroundroundroundround10
roundroundroundroundroundround10
roundroundroundroundround10
roundroundroundround10
roundroundround10
roundround10
round10

 

I'm kinda stuck here. Is it because I'm trying to replace numbers?

 

Regards,

Chris

Link to comment
https://forums.phpfreaks.com/topic/239919-replacing-numbers/
Share on other sites

I've fixed it another way, saving me a lot of other codes by combining some functions. But still, I'm wondering how this can fail. All I want is to use a variable, for example (it doesn't work):

$currentround = $round.$i;

The 'currentround' is depending on the $i, and I want to add the $i to the variablestring (lol?). I hope you get what I mean...

 

Thanks,

Chris

Link to comment
https://forums.phpfreaks.com/topic/239919-replacing-numbers/#findComment-1232409
Share on other sites

Whatever you are trying to do this is probably not the correct way, but in case I am wrong, this works fine:

 

$round = 'round';

for($i=1; $i<11; $i++) {
    $currentround = $round.$i;
    echo "$currentround\n";
}

 

round1
round2
round3
round4
round5
round6
round7
round8
round9
round10

Link to comment
https://forums.phpfreaks.com/topic/239919-replacing-numbers/#findComment-1232412
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.