0

I need a function like this:

$foo = "bar";

$arr['bar'] = "foo";


myfunc(array($foo,$arr['bar'])); // return array('foo','bar')

Any Ideas how to achieve this? Is this even possible in PHP?

ali smith
  • 93
  • 2
  • 2
  • 9
  • 2
    Too many "foo"s and "bar"s. Clean up what you're trying to ask. – Ignacio Vazquez-Abrams Oct 11 '12 at 23:00
  • 1
    looks to me like your function should return the array in the opposite order, so... `// return array('bar','foo')` – Billy Moon Oct 11 '12 at 23:02
  • There are any number of ways this function could be designed, because it is not well defined enough. Try to think of the function as a [black box](http://en.wikipedia.org/wiki/Black_box). – Asad Saeeduddin Oct 11 '12 at 23:03
  • It's possible. What exactly do you need it to do? You certainly can pass an array to a function, if that's what you're asking. – Simon Germain Oct 11 '12 at 23:04
  • my question like this http://stackoverflow.com/questions/255312/how-to-get-a-variable-name-as-a-string-in-php/12849093 but i want to use both array and variable, for example : `$arr['foo']="bar"; myfunc($arr['foo']); //return array key foo` – ali smith Oct 11 '12 at 23:12

2 Answers2

0

You can get a variable from a string name like so:

$foo = "123";
$bar = "foo";
var_dump($$bar); // string(3) "123"

But as far as I'm aware you can't go the other way, to find a variable's name.

There should be no need to know a variable's name anyway. Use associative arrays where possible.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • my question like this http://stackoverflow.com/questions/255312/how-to-get-a-variable-name-as-a-string-in-php/12849093 but i want to use both array and variable, for example : `$arr['foo']="bar"; myfunc($arr['foo']); //return array key foo` – ali smith Oct 11 '12 at 23:13
0
<?php
 $foo = "bar";

 $arr['bar'] = "foo";

 echo $$arr['bar']; //returns bar

 myFunc(array($foo,$$arr['bar']));
?>

that would work..

user1732887
  • 288
  • 1
  • 2
  • 9