Php variable name as variable

$string = "id"; want result to be like $id = "new value"; 

How to do this in php?

Change ..

How about below?

 $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";
$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"; 
+4
source share
8 answers

Second answer in response to your edit:

 $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++; } } 

This will cover your result using the $i counter as the key for your resulting arrays.

EDIT

Additional answer in response to your comment:

 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; } 

This code creates a temporary multidimensional array for storing column names and loop values ​​around this array to create variable array variables. As a side, I should not have used the temp array, since $$column_name[$i] does not work, I would like to see alternative answers to this problem.

Final note @ Paisal, I see you never accepted the answer, I wouldn’t have put in much effort if I had seen this before!

+1
source

Theres 2 basic methods

The first is the double $ ( variable variable ) so

 $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

And the second way is to use {} lik, therefore

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

You can also do:

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

I had a game with this on streamlined objects a few weeks ago and got interesting results.

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

You are looking for Variables

 $$string = "new value"; 

let you call

 echo $id; // new value 

Later in the script

+3
source

You can do it

 $$string = "new value"; 

juste double $

+1
source

Do you mean variable variables ?

This will do something like this:

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

This creates a variable $id with the value "new value" .

0
source

Do not do this. Just use an array.

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

ref: How to create a dynamic variable with PHP?

0
source

Try the following:

 $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
source

For those of us who need things explained in great detail ...

 // 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'; 
0
source

Source: https://habr.com/ru/post/1335061/


All Articles