1

For instance, suppose I had 3 columns in my database: column1, column2, column3

And I had 3 variables with the same name as that of the columns:

$column1 = 'Column 1 value';
$column2 = 'Column 2 value';
$column3 = 'Column 3 value';

So if I appended a '$' sign to the column names in a foreach loop, I'll get the variable names.

$string = "column1, column2, column3";

function get_variable_names($string) {
    $array = explode(", ", $string); // Convert to array

    foreach ($array as $key => $value) {
        $array_values_with_dollar[] = '$'.$value;
    }
    $imploded = implode(", ", $array_values_with_dollar); // Convert to string again
    return $imploded;
}

$variable_string = get_variables($string);

Now, the value of $variable_string would be: $column1, $column2, $column3. (string).

Would it be possible to output the real value of the variables such that:

$variable_string => Column 1 value, Column 1 value, Column 1 value

[ without using eval() ]

4 Answers 4

3

You can call them like this:

echo ${'column1'};

So if you want to do a loop:

for($i=1; $i<=3; $i++)
    echo ${'column' . $i} . '<br>';

If you want it with a foreach, you can do it like this:

$string = 'column1, column2, column3';
$variables = explode(', ', $string);

foreach($variables as $variable)
    echo $$variable . '<br>';

Result

Column 1 value
Column 2 value
Column 3 value

For more information: http://php.net/manual/en/language.variables.variable.php

Sign up to request clarification or add additional context in comments.

2 Comments

How to return add $$variable values to an array? eg.: $name= '1 value'; $date= '2 value'; $email= '3 value'; $string = 'name, date, email'; $variables = explode(', ', $string); $cesta = array(); array_push($cesta, $variables); $G_line = $cesta; $G_line_string=implode("|", $G_line); // return 1 value|2 value|3 value ?????
@user3768564 Simply use foreach ($variables as $variable) { array_push($cesta, $$variable); }
1

Just use $$ instead of $

$column1 = 'Column 1 value';
$column2 = 'Column 2 value';
$column3 = 'Column 3 value';

$string = "column1, column2, column3";
$array = explode(", ", $string);
foreach ($array as $elemnt){
    echo $$elemnt . "<br>";
}
exit;

outputs

Column 1 value
Column 2 value
Column 3 value

Comments

1

i think , you may use this code

$column1 = 'Column 1 value';
$column2 = 'Column 2 value';
$column3 = 'Column 3 value';

$string = "column1, column2, column3";

function get_variable_names($string) {
    $array = explode(", ", $string);         
    return $array;
}

$variable_array = get_variable_names($string);



foreach($variable_array as $str){
    echo $$str.'<br/>'; 
}

Comments

0

Use list to list the variables and explode on delimiter.

List($column1, $column2, $column3) = explode(", ", "column1, column2, column3"); 

https://3v4l.org/IMAoQ

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.