1. <?php
  2. //compact 将变量组合为数组
  3. $key_1 = "val_1";
  4. $key_2 = "val_2";
  5. $data = array('$key_3'=>'val_3','$key_4'=>'val_4');
  6. $arr_n =compact('key_1','key_2','data');
  7. echo "compact:\n";
  8. //extract根据键名导出变量
  9. echo "extract:\n";
  10. $arr_str = array('key_a'=>'val_a','key_b'=>'val_b','key_arr'=>array('1','2','3'));
  11. extract($arr_str);
  12. var_dump($key_a);
  13. var_dump($key_b);
  14. var_dump($key_arr);
  15. // list(只作用于索引数组)用来取出数据。可只取出部分数据,如只取出前两个
  16. echo "list:\n";
  17. $arr_data =array('data_1','data_2','data_3','data_4');
  18. list($v1,$v2)=$arr_data;
  19. var_dump($v1);
  20. var_dump($v2);
  21. //list也可取变量放在数组指定位置中
  22. list($data_res['0'],$data_res['5'])=$arr_data;
  23. var_dump($data_res);
  24. ?>

compact 多个变量数组

//多个变量数组

    $name='phpff';

    $email='phpff@phpff.com';

    $info=compact('name','email');//传递变量名

    print_r($info);

    /*

    Array

    (

        [name] => phpff

        [email] => phpff@phpff.com

    )

    */

extract 数组多个变量

//数组多个变量

    $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