2013-04-04 142 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