Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP join two variable names

I have a php script that gets a $_POST to decide which array to return. Ex:

$n = $_POST['n']; // 1, 2 or 3

$a1 = array ('something', 'something else', 'another thing');

$a2 = array ('something 2', 'something else 2', 'another thing 2');

$a3 = array ('something 3', 'something else 3', 'another thing 3');

now I want to get the array that corresponds to the $n value, let's say "2".

How can I say echo $a . $n to get $a2

Thanks.

like image 562
medk Avatar asked Sep 01 '11 06:09

medk


2 Answers

${'a'.$n} gives you $a2 if $n is 2.

like image 127
xdazz Avatar answered Nov 08 '22 23:11

xdazz


It would be better that you make as this:

$a = array();

$a[1] = array('bla bla', 'bla bla');
$a[2] = array('asdasd', 'asdasd');

And then you can call as this:

echo $a[intval($n)]
like image 30
Ernestas Stankevičius Avatar answered Nov 09 '22 01:11

Ernestas Stankevičius