当前位置:网站首页>Use of compact, extract and list functions in PHP

Use of compact, extract and list functions in PHP

2022-06-13 03:15:00 wks19891215

<?php
//compact  Combine variables into an array 
$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 Export variables based on key names 
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( Only works on indexed arrays ) Used to fetch data . Only part of the data can be extracted , If only the first two are taken out 
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 Variables can also be placed in the specified position of the array 
list($data_res['0'],$data_res['5'])=$arr_data;
var_dump($data_res);
 ?>

原网站

版权声明
本文为[wks19891215]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280532253878.html