php 项目程序如何将数组转化为一组变量?

2023-02-17 奥古斯宏

extract和compact函数。

extract将数组导入为众多变量,数组的键作为变量名,数组的值作为变量值。

例如:

$size = "large";
$var_array = array("color" => "blue",
                   "size"  => "medium",
                   "shape" => "sphere");
extract($var_array, EXTR_PREFIX_SAME, "wddx");

可以通过传入第二个参数,处理当变量有冲突时如何处理。当遇到冲突时,可以增加前缀,此时可以传入第三个参数作为前缀。

compact刚好相反,将变量组成数组。

例如:

<?php
$city  = "San Francisco";
$state = "CA";
$event = "SIGGRAPH";

$location_vars = array("city", "state");

$result = compact("event", $location_vars);
print_r($result);

版权声明:本文由phpreturn.com(PHP武器库官网)原创和首发,所有权利归phpreturn(PHP武器库)所有,本站允许任何形式的转载/引用文章,但必须同时注明出处。

最近浏览
IP用户:3.220.*.*
2 天前 Amazon Bot
IP用户:51.222.*.*
6 天前 aHrefs Bot
IP用户:60.184.*.*
7 天前 Internet Explorer Windows 7
IP用户:125.104.*.*
7 天前 Chrome Windows 8.1
IP用户:117.90.*.*
7 天前 Baidu Spark Windows 7
IP用户:47.82.*.*
7 天前 Microsoft Edge Windows 10
IP用户:123.97.*.*
7 天前 Sogou Explorer Windows 7
IP用户:47.79.*.*
8 天前 Microsoft Edge Windows 10
IP用户:112.86.*.*
8 天前 Sogou Spider
IP用户:175.155.*.*
9 天前 UBrowser Windows 7
IP用户:183.164.*.*
10 天前 Chrome Windows 2000
IP用户:180.117.*.*
15 天前 UBrowser Windows XP
累计浏览次数:1196
评论
点击登录
phpreturn,PHP武器库,专注PHP领域的项目和资讯,收录和介绍PHP相关项目。
最近浏览 点击登录
累计浏览次数:216589
一周浏览次数:1295
今日浏览次数:37

本站所有权利归 phpreturn.com 所有

举报/反馈/投稿邮箱:phpreturn@ulthon.com

鲁ICP备19027671号-2