Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[Help] [Solved] Fuse 2 variables into 1

Skilled Illusionist
Joined
May 10, 2013
Messages
321
Reaction score
59
Hi, I have a problem, and I'd like some help.

PHP:
<?php include 'test.php';?>
<?php
for($i=1;$i<=10;$i++){
echo($x1[0]);
echo($x1[1]);
echo($x1[2]);
}
?>


In the include file are just some text references:
PHP:
<?php
$x1 = array();
$x1['0'] = "Hello 1.0";
$x1['1'] = "Hello 1.1";
$x1['2'] = "Hello 1.2";

$x2 = array();
$x2['0'] = "Hello 2.0";
$x2['1'] = "Hello 2.1";
$x2['2'] = "Hello 2.2";
?>
And so on...


How do I get the number after $x in the loop changed when the loopnumber changes.
Like "echo($x($i)[0]);" obviously doesn't work. I can't figure out the right way to do this.

Help is appreciated.

Thank you.


Solution:
PHP:
<?php include 'test.php';?>
<?php
for($i=1;$i<=10;$i++){
echo(${'x'.$i}[0]);
echo(${'x'.$i}[1]);
echo(${'x'.$i}[2]);
}
?>
 
Last edited:
Elite Diviner
Joined
May 30, 2011
Messages
443
Reaction score
95


Code:
<?php
// x_array contains $x0 .. $x10
foreach ($x_array as $str_rsrcs) {
   foreach ($str_rsrcs as $resource_str) {
       echo $resource_str;
    }
}
?>

Sprintf is a good alternative to a table of 99% identical strings.

Using strings as an index into a variable is a terrible idea, unless it's an actual index (e.g. $x[$i]). Your solution will work as long as there are variables named $x1 through $x10 and ins't super vulnerable to attacks because the string is a constant "x" in this case, but generally this is a fast way to introduce bugs. Why not use an array of arrays?
 
Last edited:
Back
Top