In php, we can use the extract or compact function to convert arrays and variables. Let's share the two instances with you.
Compact multiple variables to the array
| The Code is as follows: |
Copy code |
|
<? Php
// 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: |
Copy code |
|
<? Php
// 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: |
Copy code |
|
<? 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