Hello I am trying to convert the value of an array to a variable.
I have a class named X as follows:
class x
{
public static function getData()
{
$data = array(
"start" => '$id',
"end" => '$name'
);
return $data;
}
}
The getData()
method returns the array:
array (size=2)
'start' => string '$id' (length=3)
'end' => string '$name' (length=5)
I have function as follows in another class:
$id = 10;
$name = "kheshav";
$data = x::getData();
var_dump($data);
What I want is to resolve the $id
and $name
values in the array to its corresponding variable, so that end result will be as follows:
array (size=2)
'start' => int 10
'end' => string 'kheshav' (length=7)
I tried the following code but with no hope:
foreach ($data as $key => $value) {
$data[$key] = eval("\$value =\"$value\";");
}
Well the conventional way to do this would be to pass the data into the function:
class x{
public static function getData($id,$name){
$data = array(
"start" => $id,
"end" => $name
);
return $data;
}
}
$id=10;
$name = "kheshav";
$data = x::getData($id,$name);