本文實例講述了php變量與數組相互轉換的方法。分享給大家供大家參考,具體如下:
在php中數組與變量相互轉換我們可使用到extract或compact函數,這里就來給大家分析一下這兩個函數的用法。
compact 多個變量轉數組
<?php //多個變量轉數組 $name='jb51'; $email='jb51@vevb.com'; $info=compact('name','email');//傳遞變量名 print_r($info); /* Array ( [name] => jb51 [email] => jb51@vevb.com ) */?>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
希望本文所述對大家PHP程序設計有所幫助。
新聞熱點
疑難解答
圖片精選