Php array processing function extract and instance code _ php instance

Source: Internet
Author: User
The phpextract function uses the array key name as the variable name and the array key value as the variable value. This function can process form submission and insert data into the database. This article explains the basic usage and examples of the extract function. For more information, see Php array processing function extract

The extract function is used to import variables from an array to the current symbol table.

Basic syntax

Int extract (array & $ var_array [, int $ extract_type = EXTR_OVERWRITE [, string $ prefix = NULL])

This function is used to import variables from the array to the current symbol table. Check each key name to see if it can be used as a valid variable name, and check for conflicts with existing variable names in the symbol table.

Parameter introduction:

Parameters Description
Var_array Required. Specifies the array to be used.

An associated array. This function regards the key name as the variable name and the value as the value of the variable. Each key/value pair will create a variable in the current symbol table and be affected by the extract_type and prefix parameters.

The associated array must be used. the array of the numeric index will not produce results unless EXTR_PREFIX_ALL or EXTR_PREFIX_INVALID is used.

Extract_type

Optional. The extract () function checks whether each key name is a valid variable name and whether it conflicts with an existing variable name in the symbol table. The processing of invalid and conflicting key names depends on this parameter.

Possible values:

  • EXTR_OVERWRITE-default. If a conflict exists, the existing variables are overwritten.
  • EXTR_SKIP-if there is a conflict, the existing variables are not overwritten.
  • EXTR_PREFIX_SAME-if there is a conflict, add the prefix before the variable name.
  • EXTR_PREFIX_ALL-prefix all variable names.
  • EXTR_PREFIX_INVALID-prefix is added only before invalid or numeric variable names.
  • EXTR_IF_EXISTS-only overwrite the values of variables with the same name in the current symbol table. None of them are processed.
  • EXTR_PREFIX_IF_EXISTS.
  • EXTR_REFS-extract variables as references. The imported variable still references the value of the array parameter.
Prefix

Optional. Note that prefix is only required when the value of extract_type is EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS. If the result with a prefix is not a valid variable name, it is not imported to the symbol table. An underline is automatically added between the prefix and the array key name.

Return value

Returns the number of variables successfully imported to the symbol table.

Instance:

<?php$size = "large";$var_array = array(  "color" => "blue",  "size" => "medium",  "shape" => "sphere");extract($var_array, EXTR_PREFIX_SAME, "wddx");echo " $color , $size , $shape , $wddx_size 
";?>

Running result:

Blue, large, sphere, medium

Thank you for reading this article. I hope it will help you. thank you for your support for this site!

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.