PHP join two variable names

自古美人都是妖i 提交于 2019-11-30 15:18:07

问题


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.


回答1:


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




回答2:


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)]


来源:https://stackoverflow.com/questions/7266754/php-join-two-variable-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!