compact 多個變量轉數組
代碼如下 復制代碼<?php
//多個變量轉數組
$name='phpff';
$email='[email protected]';
$info=compact('name','email');//傳遞變量名
print_r($info);
/*
Array
(
[name] => phpff
[email] => [email protected]
)
*/
?>
extract 數組轉多個變量
代碼如下 復制代碼<?php
//數組轉多個變量
$capitalcities['England'] = 'London';
$capitalcities['Scotland'] = 'Edinburgh';
$capitalcities['Wales'] = 'Cardiff';
extract($capitalcities);//轉變成三個變量 England,Scotland,Wales
print $Wales;//Cardiff
?>
例
代碼如下 復制代碼<?php
$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