- <?php
- //compact 将变量组合为数组
- $key_1 = "val_1";
- $key_2 = "val_2";
- $data = array('$key_3'=>'val_3','$key_4'=>'val_4');
- $arr_n =compact('key_1','key_2','data');
- echo "compact:\n";
- //extract根据键名导出变量
- echo "extract:\n";
- $arr_str = array('key_a'=>'val_a','key_b'=>'val_b','key_arr'=>array('1','2','3'));
- extract($arr_str);
- var_dump($key_a);
- var_dump($key_b);
- var_dump($key_arr);
- // list(只作用于索引数组)用来取出数据。可只取出部分数据,如只取出前两个
- echo "list:\n";
- $arr_data =array('data_1','data_2','data_3','data_4');
- list($v1,$v2)=$arr_data;
- var_dump($v1);
- var_dump($v2);
- //list也可取变量放在数组指定位置中
- list($data_res['0'],$data_res['5'])=$arr_data;
- var_dump($data_res);
- ?>
compact 多个变量转数组
//多个变量转数组
$name='phpff';
$email='phpff@phpff.com';
$info=compact('name','email');//传递变量名
print_r($info);
/*
Array
(
[name] => phpff
[email] => phpff@phpff.com
)
*/
//数组转多个变量
$capitalcities['England'] = 'London';
$capitalcities['Scotland'] = 'Edinburgh';
$capitalcities['Wales'] = 'Cardiff';
extract($capitalcities);//转变成三个变量 England,Scotland,Wales
print $Wales;//Cardiff
$my_array = array("a" => "Cat","b" => "Dog", "c" => "Horse");
extract($my_array);
echo "$a = $a; $b = $b; $c = $c";
结果
$a = Cat; $b = Dog; $c = Horse
本文来自博客园,作者:孙龙-程序员,转载请注明原文链接:https://www.cnblogs.com/sunlong88/articles/8681430.html