1

imagine that I have 3 variables in a array:

 $var1 = "a";
 $var2 = "b";
 $var3 = "c";
 $a = array($var1, $var2, $var3);
 foreach($a as $var)
 {
    //code that create strings with variable names and echo var1 , var2 , var3
 }

Is this posible to do that with php?

Soroush Khosravi
  • 180
  • 1
  • 2
  • 13

4 Answers4

4

If you're looking to see if you can recall the variable names from the array that you created, I'm sorry to tell you this but it's not going to be possible (at least, in the way your example has it). When you're creating that array, you're making a copy of those variables and storing them in the array. You aren't actually pointing to the original variables.

Couple of options of the top of my head.

Option 1: Create the array with the variable names as the keys.

Option 2: Assign the variable's name to the variable itself beforehand. Ex: $var1->VAR_NAME = 'var1';

Option 3: Try a workaround like the one suggested here: How to get a variable name as a string in PHP?

Option 4: If you just need it to reference the original variable, try capturing the reference to the object itself using &.

Hope that helps.

Community
  • 1
  • 1
sgcharlie
  • 1,006
  • 1
  • 10
  • 25
3

When you run $a = array($var1 .., those values are written to $a, and the original symbol table name, $var1 is kept separately I believe. That means that no, there is no way to get the original variable name at this time. You have to do it ahead of time, or do some crazy black magic with get_defined_vars:

$var1 = 'a';
...
$varnames = array('var1', 'var2', 'var3');
$a = compact($varnames);
//$a now has array('var1' => 'a' ...
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
1

If you can store your variables like this:

   $array = array(
    'a' => 'apple',
    'b' => 'banana',
    'c' => 'carrot',
    'd' => 'date',
    'e' => 'elephant');

You can use the key function to retrieve the index/key for the current array element inside any type of loop, see #1, or use the key => value syntax in your foreach() loop, see #2:

<?php
   $array = array(
    'a' => 'apple',
    'b' => 'banana',
    'c' => 'carrot',
    'd' => 'date',
    'e' => 'elephant');

    /* #1 */
    foreach($array as $value) {
       /* get the key for the item at the cursor position of the array */
       $key = key($array);
       print "{$key} = {$value}<br>";
       /* move the cursor to the next item in the array */
       next($array);
    }

    /* #2 */
    foreach($array as $key => $value) {
       print "{$key} = {$value}<br>";
    }

?>

If you are simply trying to get a variable name, you can not store the values in an array and maintain the variable names, once you put the values into the array, they lose the variable name.

Outside of an array, you can use the following solution (from http://www.php.net/manual/en/language.variables.php#76245):

<?
  $myVarName = "myValue";
  echo var_name($myVarName) . " = {$myVarName}";

  function var_name(&$var, $scope=0)
     {
       $old = $var;
       if (($key = array_search($var = 'unique'.rand().'value', !$scope ? $GLOBALS : $scope)) && $var = $old) return $key; 
     }
?>
Larry Williamson
  • 1,149
  • 5
  • 18
0
$a = array($var1, $var2, $var3);
foreach($a as $key => $value) {
    echo($key . ' => ' . $value);
}
Gabriel Santos
  • 4,934
  • 2
  • 43
  • 74