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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.