Methods for converting php variables and arrays (extract and compact), extractcompact
This article describes how to convert php variables and arrays. We will share this with you for your reference. The details are as follows:
In php, we can use the extract or compact function to convert arrays and variables. Here we will analyze the usage of these two functions.
Compact multiple variables to the array
<? Php // convert multiple variables to an array $ name = 'jb51 '; $ email = 'jb51 @ jb51.net'; $ info = compact ('name', 'email '); // pass the variable name print_r ($ info);/* Array ([name] => jb51 [email] => jb51@jb51.net) */?>
Convert the extract array to multiple variables
<? Php // convert the array to multiple variables $ capitalcities ['England '] = 'London'; $ capitalcities ['scotland'] = 'edinburgh '; $ capitalcities ['Wales '] = 'cardiff'; extract ($ capitalcities); // convert it into three variables: England, Scotland, Wales print $ Wales; // Cardiff?>
Example:
<? Php $ my_array = array ("a" => "Cat", "B" => "Dog", "c" => "Horse"); extract ($ my_array ); echo "$ a = $ a; $ B = $ B; $ c = $ c";?>
Result:
$ A = Cat; $ B = Dog; $ c = Horse