dongqing904999 2018-01-21 20:54
浏览 19
已采纳

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

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!

  • 写回答

1条回答 默认 最新

  • duanchuan6350 2018-01-21 20:56
    关注

    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"];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?