Methods for converting php variables and arrays (extract and compact), extractcompact

Source: Internet
Author: User

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

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.