当字符串的一部分是变量时,打印一个键数组

问题描述:

I am a beginner in php, and I would like to do something very simple : I have :

$names: 
0 => test0
1 => test1
2 => test2

$_POST[arraytest0] => bla0
$_POST[arraytest1] => bla1
$_POST[arraytest2] => bla2

and what I want to do is to print bla0, 1 and 2 using a loop, but I don't know the syntax. For example,

echo $_POST['array.$names[0]'];

doesn't work. Thank you!

You can just concatenate the string literal (that needs quotes) with the variable (that doesn't):

echo $_POST['array' . $names[0]];

In a loop that becomes:

foreach($names as $name) {
    echo $_POST['array' . $name];
}

Or with double quotes you can embed the variable in the string, which will be evaluated:

foreach($names as $name) {
    echo $_POST["array$name"];
}