2011-01-11 150 views
4
 

$string = "id"; 

want result to be like 

$id = "new value"; 
 

如何在PHP中對此進行編碼?PHP字符串名稱作爲變量

編輯..

下面怎麼樣?

 

$column = array("id","name","value"); 

let say found 3 row from mysql 

want result to be like this 

$id[0] = "3"; 
$id[1] = "6"; 
$id[2] = "10"; 

$name[0] = "a"; 
$name[1] = "b"; 
$name[2] = "c"; 

$value[0] = "bat"; 
$value[1] = "rat"; 
$value[2] = "cat"; 

 
+0

雖然PHP也允許你這樣做(如下顯示的答案)它通常被認爲是一個糟糕的設計原則,它會讓你的代碼難以維護。更好的方法可能是使用關聯數組,例如`$ my_array ['id'] =「new value」;` – Gareth 2011-01-11 10:47:16

+1

您將不得不爲更新創建一個單獨的問題。 – RobertPitt 2011-01-11 11:41:16

回答

1

響應您的編輯第二個答案:

$result = mysql_query($sql); 
$num = mysql_num_rows($result); 
$i = 0; 
$id = array(); 
$name = array(); 
$value = array(); 

if ($num > 0) { 
    while ($row = mysql_fetch_assoc($result)) { 
    $id[$i] = $row['id']; 
    $name[$i] = $row['name']; 
    $value[$i] = $row['value']; 
    $i++; 
    } 
} 

這個循環在你的結果,使用計數器$i爲你的結果陣列的關鍵。

編輯

附加響應您的評論的答案:

while ($row = mysql_fetch_assoc($result)) { 
    foreach($row as $column_name => $column_value) { 
    $temp_array[$column_name][$i] = $column_value; 
    } 
    $i++; 
} 

foreach ($temp_array as $name => $answer) { 
    $$name = $answer; 
} 

此代碼創建一個臨時多維數組來保存列名和值圍繞陣列的循環來創建您的變量變量數組。作爲一方,我不得不使用temp數組作爲$$column_name[$i]不起作用,我很想看到這個問題的替代答案。

最後說明@佩薩爾,我看到你從未接受過答案,如果以前我看到過,我不會付出這麼多努力!

3

您正在尋找Variable Variables

$$string = "new value"; 

會讓你叫

echo $id; // new value 

後來在腳本

1

你可以做到這一點

$$string = "new value"; 

juste double $

0

您指的是variable variables

這將完成這樣的事情:

$string = "id"; 
$$string = "new value"; 

這將產生一個變量$id與價值"new value"

8

即使世界2種主要方法

首先是雙$Variable Variable)像這樣

$var = "hello"; 
$$var = "world"; 
echo $hello; //world 

//You can even add more Dollar Signs 

$Bar = "a"; 
$Foo = "Bar"; 
$World = "Foo"; 
$Hello = "World"; 
$a = "Hello"; 

$a; //Returns Hello 
$$a; //Returns World 
$$$a; //Returns Foo 
$$$$a; //Returns Bar 
$$$$$a; //Returns a 

$$$$$$a; //Returns Hello 
$$$$$$$a; //Returns World 

//... and so on ...// 

@source

,第二種方法是使用{} LIK所以

$var = "hello"; 
${$var} = "world"; 
echo $hello; 

您也可以這樣做:

${"this is a test"} = "works"; 
echo ${"this is a test"}; //Works 

我對這個在流播放對象在幾個星期前,並得到了一些有趣的結果

$Database->Select->{"user id"}->From->Users->Where->User_id($id)->And->{"something > 23"}; 
+0

謝謝我有新的問題(編輯上面) – Paisal 2011-01-11 10:59:38

0

不要那樣做。只需使用一個數組。

$arr[$string] = 'new value'; 

裁判:How do I build a dynamic variable with PHP?

+0

不要做變量變量?如果你不介意我問,你有什麼推理呢? – 2011-01-11 10:50:13

0

試試這個:

$result = mysql_query($sql); 
$num_rows = mysql_num_rows($result); 
$i = 0; 

if ($num_rows) { 
    while ($row = mysql_fetch_assoc($result)) { 
    foreach($row AS $key => $value) { 
     ${$key}[$i] = $value; 
    } 

    $i++; 
    } 
} 
0

對於我們這些誰需要的東西在非常詳細的解釋...

// Creates a variable named '$String_Variable' and fills it with the string value 'id' 
$String_Variable = 'id'; 

// Converts the string contents of '$String_Variable', which is 'id', 
// to the variable '$id', and fills it with the string 'TEST' 
$$String_Variable = 'TEST'; 

// Outputs: TEST 
echo $id; 

// Now you have created a variable named '$id' from the string of '$String_Variable' 
// Essentially: $id = 'Test';