Php variable and array conversion (extract and compact) _ PHP Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.