Home >Backend Development >PHP Tutorial >PHP uses the key name in the array as the variable name and the key value as the variable_PHP tutorial

PHP uses the key name in the array as the variable name and the key value as the variable_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 10:49:57923browse

This article will introduce you to how PHP uses the key names in the array as variable names and the key values ​​as variables. I hope this tutorial will be helpful to all students.

 代码如下 复制代码
/*
php 把数组中的键名所为变量名键值作为变量
*/
$arr=array('a'=>1,'b'=>2,'c'=>3,'d'=>5,'e'=>6);
//方法一,使用foreach循环实现
foreach($arr as $key=>$value){
 $$key=$value;
}
 
echo $a;
 
//方法二,其实php早就已经实现了这个功能extract
extract($arr);
echo $a;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632671.htmlTechArticleThis article introduces to students how to use the key name in the array as the variable name and the key value as the variable. I hope this tutorial It will be helpful to all classmates. The code is as follows. Copy the code ?php /* php put...
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