Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中动态地向变量追加内容?_Php_Variables - Fatal编程技术网

如何在PHP中动态地向变量追加内容?

如何在PHP中动态地向变量追加内容?,php,variables,Php,Variables,我有一张这样的表格: <?php if (isset($_POST['artist'])) { // do something } ?> <form name="admin_on_artist_<?php echo $artist->ID; ?>" action="" method="POST"> <p class="artist-negative"> <label for="artist"&

我有一张这样的表格:

<?php if (isset($_POST['artist'])) {
      // do something
    } ?>
<form name="admin_on_artist_<?php echo $artist->ID; ?>" action="" method="POST">
    <p class="artist-negative">
        <label for="artist"><input type="checkbox" name="artist_<?php echo $artist->ID; ?>" id="artist_<?php echo $artist->ID; ?>"> Check this?</label>
    </p>
    <button type="submit">Update</button>
</form>


您可以将生成前端表单标记的
foreach
与处理表单提交的
foreach
配对。比如:

<?php
$regex = '/^artist_([0-9]+)$/'
foreach (array_keys($_POST) as $key) {
    if (preg_match($regex,$key,$matches)) {
        $artistId = (int)$matches[1];
        // do something with $_POST[$key] according to $artistId
    }
}

这个问题的解决方案比我一开始能掌握的要简单得多,但基本上我只需要这么做,这与我最初的问题的关键区别在于前两行:

<?php $artist_form_id = 'artist_'.$artist->ID;
if (isset($_POST[$artist_form_id])) {
  // do something
} ?>
<form name="admin_on_artist_<?php echo $artist->ID; ?>" action="" method="POST">
    <p class="artist-negative">
        <label for="artist"><input type="checkbox" name="artist_<?php echo $artist->ID; ?>" id="artist_<?php echo $artist->ID; ?>"> Check this?</label>
    </p>
    <button type="submit">Update</button>
</form>


一个id为的隐藏表单输入怎么样?