Convert php variables to Arrays (extract and compact ). In php, we can use the extract or compact function to convert arrays and variables. let's share the two instances with you. Compact: convert multiple variables to Arrays. in php, convert arrays and variables to each other. we can use the extract or compact functions. let's share these two functions with you.
Compact multiple variables to the array
The code is as follows: |
|
// Convert multiple variables to an array $ Name = 'phpff '; $ Email = 'phpff @ phpff.com '; $ Info = compact ('name', 'Email '); // pass the variable name Print_r ($ info ); /* Array ( [Name] => phpff [Email] => phpff@phpff.com ) */ ?> |
Convert the extract array to multiple variables
The code is as follows: |
|
// Convert an 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
The code is as follows: |
|
$ 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
Bytes. Compact multiple variables to the array...