Php 将变量名称解析为字符串文字,以将字符串的一部分分配给新的变量

Php 将变量名称解析为字符串文字,以将字符串的一部分分配给新的变量,php,preg-replace,Php,Preg Replace,我将数据库表idnumber附加到动态呈现的按钮,如下所示: echo '</div><!-- close formRowDiv -->'; echo '<div class="buttonDiv">'; echo '<input type="submit" id="buttonUpdate_' . $row[ 'id' ] . '" name="buttonUpdate_' . $row[ 'id' ] . '" class="button" valu

我将数据库表
id
number附加到动态呈现的按钮,如下所示:

echo '</div><!-- close formRowDiv -->';
echo '<div class="buttonDiv">';
echo '<input type="submit" id="buttonUpdate_' . $row[ 'id' ] . '" name="buttonUpdate_' . $row[ 'id' ] . '" class="button" value="Update Trouble Log Entry">';
echo '</div><!-- closes updateDiv -->';
echo';
回声';
回声';
回声';
因此,当单击时,我将以
$\u POST['buttonUpdate\u 1']
结束,例如作为引用的DB表中的id#1。现在我想从$u POST var名称中提取1


我假设我需要使用
preg_replace
,但无法考虑如何在模式中使用var名称来提取附加的id。我一直在谷歌上搜索,似乎也找不到我真正需要的东西。因此,如何在
preg\u replace
中将变量名称解析为文本字符串,以提取并将其部分分配给新的变量?

以数组样式命名输入字段。我不知道它的正确名字。不管怎样,它看起来是这样的:

echo '<input type="text" name="myinput[' . $row[ 'id' ] . ']">';

它将逐行返回输入字段的数据。

虽然不是像您所问的preg-replace解决方案,但如果我没有看错您的问题,您希望能够使用附加的id号对表中的行执行某些操作

如果是这样,您可以首先查询表中的最高值id,并将其用作for循环中的计数器。然后使用for循环,您可以循环遍历表中的所有行,直到在$\u POST['buttonDisplay\u 1']变量中找到/匹配附加的ID

匹配后,您可以获取返回查询行的ID号,并使用获取的ID号对该行执行任何操作。可能是这样的:

$sql_query_to_find_high_id = "SELECT * FROM table ORDER BY id DESC LIMIT 1";
  $result=mysqli_query(dbcon(), $sqlQHighId);
  while ($row=mysqli_fetch_array($result)){high_id_number=$row['id'];}
查找表中分配的最高id。然后使用该高id号作为计数器循环通过,您可以:

for($i=0; $i<$high_id_number; $i++){ // cycle thru all rows in table to find a match for ID appended to $_POST[ 'buttonDisplay_1' ]
    if(isset($_POST["buttonUpdate_$i"])){
      $id=$i;
      $sql_update_statement="UPDATE table SET column=value WHERE id='$id'";
}

($i=0;$i)的
已经成功了。但是需要比我想象的更多的东西。
for($i=0; $i<$high_id_number; $i++){ // cycle thru all rows in table to find a match for ID appended to $_POST[ 'buttonDisplay_1' ]
    if(isset($_POST["buttonUpdate_$i"])){
      $id=$i;
      $sql_update_statement="UPDATE table SET column=value WHERE id='$id'";
}