search
Homephp教程php手册PHP extract 将数组拆分成多个变量的函数

PHP extract 将数组拆分成多个变量的函数

Jun 13, 2016 pm 12:15 PM
arrayextractintphpvarfunctionvariableMultipleWilldismantlearrayofgrammar

extract()函数语法:
int extract( array $var_array [, int $extract_type = EXTR_OVERWRITE [, string $prefix ]] )
功能: extract()函数提取关联数组(对数字索引数组无效)每对key和value,生成以key为变量名、value为对应值的多组新变量。

复制代码 代码如下:


$size = "old size"; //注意最后size变量的值。
$a = array(
"color" => "red",
"size" => "XXL",
"price" => "53");
extract($a);
echo "color = $color
";
echo "size = $size
";
echo "price = $price
";
?>



结果为:

color = red
size = XXL
price = 53

通过上例发现$size的值为XXL,而不是之前的”old size”,说明默认情况下当数组中的key与已有的变量冲突时,将覆盖原来的变量。

继续介绍extract函数的后两个可选参数。

第二个参数$extract_type是用来控制当发生冲突时的处理方法,可能的值为:

EXTR_OVERWRITE : 冲突时覆盖已有的变量,默认值。
EXTR_SKIP : 不覆盖已有的变量,即不在生成该key和value对的变量。
EXTR_PREFIX_SAME : 冲突时,生成的新变量名加一个前缀字符串,其值由第三个参数指定。
EXTR_PREFIX_ALL : 对所有生成的新变量名加一个前缀字符串,其值由第三个参数指定。
EXTR_PREFIX_INVALID : 对key的值换为变量名时无效时(如key第一个字符为数字,而变量名要求第一个字符不能为数字),在变量名加一个前缀字符串,其值由第三个参数指定。
EXTR_IF_EXISTS : 只取出之前已经存在的变量。
EXTR_PREFIX_IF_EXISTS : 对EXTR_IF_EXISTS取得的变量,在其变量名加一个前缀字符串,其值由第三个参数指定。
EXTR_REFS : 以引用的方式提取变量,说明提取变量的值发生改变会影响原数组的值。

注意:在变量名加前缀字符串时,新的变量名为 PREFIX_key,而不是PREFIXkey。

复制代码 代码如下:


$a = array(
"color" => "red",
"size" => "XXL",
"price" => "53");
extract($a,EXTR_PREFIX_ALL,"SC");
echo "color = $SC_color
";
echo "size = $SC_size
";
echo "price = $SC_price
";
extract($a,EXTR_REFS);
$color="green";
echo $a['color']; //查看原数组的值
?>


结果为:
color = red
size = XXL
price = 53
green
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment