创建基于变量值数组名 [英] Create array names based on variable value

查看:101
本文介绍了创建基于变量值数组名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想是这样的<一个href=\"http://stackoverflow.com/questions/4584996/creating-variable-names-from-an-array-list\">creating从一个数组列表变量名

Basically, what I want is like this creating variable names from an array list

但它的其他方式,而不是创建正常的变量,我要创建变量值数组

But it's the other way around, instead of creating normal variables, I want to create arrays from the variable values

推荐答案

如果你想哪个变量名称映射到它们的值的数组,可以使用紧凑型(),传递变量名字符串:

If you want to make an array which maps variable names to their values, use compact(), passing variable names as strings:

$one = 1;
$two = 2;

$variables = compact('one', 'two'); // Array ( [one] => 1, [two] => 2 )

如果你想要的是包含变量的值的数组,只是进行正常的使用数组阵列()折腾你​​的变量:

If all you want is an array containing the values of your variables, just make a normal array using array() and toss your variables in:

$one = 1;
$two = 2;

$variables = array($one, $two); // Array ( [0] => 1, [1] => 2 )

这篇关于创建基于变量值数组名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆