php-获取当前变量名称并将其作为数组键

问题描述 投票:0回答:2

我想在php中动态地做这样的事情:

$varname = $clang_arr['varname'];

并且循环播放,类似:

$a = [$x, $y, $z, $w, ...];
foreach($a as $v) $v = $clang_arr['v'];

以上结果将是:

$x = $clang_arr['x']
$y = $clang_arr['y']
...

我尝试了一些操作,但是很遗憾,它没有用!我可以在php中动态地执行类似操作吗?

提前感谢

php arrays var
2个回答
0
投票

您要寻找的功能是extract。简单的例子:

extract

0
投票

根据您的评论,您似乎想要以下内容:

$arr = ['x' => 1, 'y' => 2];
extract($arr);
var_dump($x, $y);
// outputs
// int(1)
// int(2)

注意:-您可以使用$data = file_get_contents('language json file path'); // to get json data from file $array = json_decode($data,true);//convert json data to array $keys = array_keys($array);//get all the keys of array $values = array_values($array);//to get all the values 同时获取两个键,值

© www.soinside.com 2019 - 2024. All rights reserved.