Home > php教程 > php手册 > php变量与数组的相互转换(extract 与 compact)

php变量与数组的相互转换(extract 与 compact)

WBOY
Release: 2016-06-13 09:51:45
Original
1260 people have browsed it

在php中数组与变量相互转换我们可使用到extract或compact函数哦,下面我来给大家利用这两个函数来分享两个实例吧。

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

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template