使用带有数字索引的php提取功能 [英] Using php extract function with numeric indexes
问题描述
我想使用提取函数将数组值转换为变量.作为提取功能,将数组键用作变量名,将值用作变量值.我有一个带有数字键的数组,如下所示:
I want to convert array values into variables using extract function. As extract function uses array keys as variable names and values as variable values. I have an array with numeric keys as shown:
$my_array = array(0 =>"Cat", 1=>"Dog", 2=>"Horse");
extract($my_array);
我将如何在此处使用数字键作为变量名称来访问值?还是 extract()
只处理字符串键?
How i would use numeric keys here as variable names to access values? Or extract()
just deal with string keys?
推荐答案
根据手册,
您必须使用关联数组;除非您使用
EXTR_PREFIX_ALL
或EXTR_PREFIX_INVALID
,否则数字索引数组将不会产生结果.
You must use an associative array; a numerically indexed array will not produce results unless you use
EXTR_PREFIX_ALL
orEXTR_PREFIX_INVALID
.
您可以在 extract()
函数中添加前缀.在下面的示例中,这会将 var _
前缀添加到每个实例.您可以提供任何有效的变量前缀作为第三个参数-创建的变量将在 extract()
函数中反映该参数.
You can add a prefix to your extract()
function. This would, in the example below, add the var_
prefix to each instance. You can supply whatever valid variable-prefix as the third argument - the created variables would reflect that parameter in the extract()
function.
$my_array = array(0 =>"Cat", 1=>"Dog", 2=>"Horse");
extract($my_array, EXTR_PREFIX_ALL, "var");
现在可以通过 $ var_2
在 $ var_0
中找到结果.
The results can now be found in $var_0
through $var_2
.
这篇关于使用带有数字索引的php提取功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!