4

$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";


2
  • Although PHP does allow you to do this (as the answers below show), it generally considered a bad design principle and it makes your code a lot harder to maintain. A better approach is probably to use an associative array, for example $my_array['id'] = "new value"; Commented Jan 11, 2011 at 10:47
  • 1
    You will have to create a seperate question for the update. Commented Jan 11, 2011 at 11:41

8 Answers 8

9

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"};
Sign up to request clarification or add additional context in comments.

Comments

3

You are looking for Variable Variables

$$string = "new value";

will let you call

echo $id; // new value

Later in your script

Comments

1

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!

2 Comments

what if instead of define variable id,name,value manually, want to take it from $column array. I hope you under stand my point why I define $column array.
@Paisal I have updated my answer to create the column name variables dynamically. Bear in mind what @Marco Mariani said about the code being hard to read
1

You can do this

$$string = "new value";

juste double $

Comments

0

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".

Comments

0

Don't do that. Just use an array.

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

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

1 Comment

Don't do variable variables? What's your reasoning for this, if you don't mind me asking?
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.