2013-04-04 189 views
1

foreach ($_POST as $key => $value)之后我有像这样的title_1,title_2,title_3这样的变量。我需要$标题equel至$ title_ + $ ID基于两个变量的变量

我已经试过

  $link = ${$title_ . $id}; 
      $title= $title_[$id]; 
      $title= $title_[$id]; 

代码

foreach ($_POST as $key => $value) 
    { 
    if (preg_match('/^id_(\d*)$/', $value, $matches)) 
     { 
    $id = $matches[1]; 
     if (isset($_POST['title_' . $id])) 
      { 
       //$link = ${$title_ . $id}; 
       $title= $title_[$id]; 
       //mysql_query("UPDATE table SET title='" . $title . "' where id='$id'"); 

      } 
    } 
    } 

} 
+0

只要不使用'eval'。 – 2013-04-04 19:38:05

回答

7

你想要的东西像...

$link = ${$title."_" . $id}; 

或者也许

${"title_" . $id}; 

看到这个参考: http://php.net/manual/en/language.variables.variable.php

+1

难道你不是指'$ {'title_'。 $ ID};'? – nickb 2013-04-04 19:42:03

+0

仍然空输出:( – rabotalius 2013-04-04 19:48:45

+0

@rabotalius当你运行脚本时,php代码中printr($ _ POST)的输出是什么? – Tucker 2013-04-04 19:49:42