Methods of converting PHP variables to arrays (extract and compact) _php techniques

Source: Internet
Author: User
Tags compact exception handling php error

This article illustrates the method of converting PHP variables and arrays into one another. Share to everyone for your reference, specific as follows:

In PHP, the array and the variable convert to each other we can use the Extract or compact function, here to give you an analysis of the use of these two functions.

Compact multiple variable Array

<?php
  //Multiple variable-turn array
  $name = ' jb51 ';
  $email = ' jb51@jb51.net ';
  $info =compact (' name ', ' email ');//pass variable name
  print_r ($info);
  /* Array
  (
    [name] => jb51
    [email] => jb51@jb51.net
  )
  * * *
?>

Extract array to multiple variables

<?php
//array to turn multiple variables
  $capitalcities [' England '] = ' London ';
  $capitalcities [' Scotland '] = ' Edinburgh ';
  $capitalcities [' Wales '] = ' Cardiff ';
  Extract ($capitalcities)//converted into three variables england,scotland,wales
  print $Wales;//cardiff
?>

Cases:

<?php
$my _array = Array ("A" => "Cat", "B" => "Dog", "C" => "Horse");
Extract ($my _array);
echo "$a = $a; $b = $b; $c = $c ";
? >

Results:

$a = Cat; $b = Dog; $c = Horse

More about PHP Interested readers can view the site topics: "PHP common functions and Skills summary", "PHP Array" Operation Techniques Encyclopedia, "PHP string (String) Usage summary", "PHP error and Exception handling method summary", " Introduction to PHP Basic Grammar, "Introduction to PHP object-oriented Programming", "PHP+MYSQL Database Operations Introduction Tutorial" and "PHP Common database Operation tips Summary"

I hope this article will help you with the PHP program design.

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.