简体   繁体   中英

php string name as variable


$string = "id";

want result to be like 

$id = "new value";

How do I code this in php?

Edit..

How about the 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";

Theres 2 main methods

The first is the double $ ( Variable Variable ) like 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 method is to use the {} lik so

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

You can also do:

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

I had a play about with this on streamline objects a few weeks back and got some interesting results

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

You are looking for Variable Variables

$$string = "new value";

will let you call

echo $id; // new value

Later in your script

You can do this

$$string = "new value";

juste double $

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 loops around your result, using the counter $i as the key for your result 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 to hold the column names and values the loops around that array to create your variable variable arrays. As a side not I had to use the temp array as $$column_name[$i] doesn't work, I would love to see alternative answers to this problem.

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

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

Are you referring to variable variables ?

That would accomplish something like this:

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

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

Don't do that. Just use an array.

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

ref: How do I build a dynamic variable with PHP?

Try this :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM