The extract function is used to break down a number into multiple variables for direct use. The W3C explanation is as follows: the PHP extract () function imports the variables from the array to the current symbol table. For each element in the array, the key name is used for the variable name, and the key value is used for the variable value. The second parameter type is used to specify how the extract () function treats such conflicts when a variable already exists and an element with the same name exists in the array. This function returns the number of successfully set variables.
The following table describes the parameters:
Syntax
Extract (array, extract_rules, prefix)
Parameter description
Array is required. Specifies the input to be used.
Extract_rules
Optional. The extract () function checks whether each key name is a valid variable name and whether it is in conflict with the variable name in the symbol table.
The processing of illegal, numbers, and conflicting key names is determined by this parameter. It can be one of the following values:
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. (Ignore elements with the same name in the array)
EXTR_PREFIX_SAME-if there is a conflict, add the prefix before the variable name. Since PHP 4.0.5, this also includes processing digital indexes.
EXTR_PREFIX_ALL-prefix all variable names (the third parameter ).
EXTR_PREFIX_INVALID-only prefix before invalid or numeric variable names. This mark is newly added to PHP 4.0.5.
EXTR_IF_EXISTS-only overwrite the values of variables with the same name in the current symbol table. None of them are processed. It can be used for variables that have defined a combination, and then extract values from an array such as $ _ REQUEST to overwrite these variables. This mark is newly added to PHP 4.2.0.
EXTR_PREFIX_IF_EXISTS. This mark is newly added to PHP 4.2.0.
EXTR_REFS-extract variables as references. This effectively demonstrates that the imported variable still references the value of the var_array parameter. This flag can be used independently OR in extract_type OR with any other flag. This mark is newly added to PHP 4.3.0.
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.
This is useful when retrieving a row of data from the database. Let's take a look at the following example.
Copy code
<? Php
$ Db = mysql_connect ('localhost', 'root', 'ctrip07185419 ') or die ('can not connect to mysql ');
Mysql_select_db ('moviesite ', $ db) or die (mysql_error ($ db ));
Mysql_query ('set names gbk', $ db );
If (isset ($ _ GET ['action']) & $ _ GET ['action'] = 'edit ')
{
$ Query = 'select movie_name, movie_type, movie_year, movie_leadactor, movie_director FROM movie WHERE movie_id = '. $ _ GET ['id'];
// Echo $ query;
$ Result = mysql_query ($ query, $ db) or die (mysql_error ($ db ));
Extract (mysql_fetch_assoc ($ result ));
}
Else
{
$ Movie_name = '';
$ Movie_type = 0;
$ Movie_year = date ('y ');
$ Movie_leadactor = 0;
$ Movie_director = 0;
}
?>
<Html>
<Head>
<Title> <? Php echo ucfirst ($ _ GET ['action']);?> Movie </title>
<Style type = "text/css"> </style>
</Head>
<Body>
<Form action = "commit. php? Action = <? Php echo $ _ GET ['action'];?> & Type = movie "method =" post ">
<Table>
<Tr>
<Td> Movie Name </td>
<Td> <input type = "text" name = "movie_name" value = "<? Php echo $ movie_name;?> "/> </Td>
</Tr>
<Tr>
<Td> Movie Type </td>
<Td> <select name = "movie_type" id = "">
<? Php
$ Query = 'select movietype_id, movietype_label from movietype order by movietype_label ';
$ Result = mysql_query ($ query, $ db) or die (mysql_error ($ db ));
While ($ row = mysql_fetch_assoc ($ result ))
{
If ($ row ['movietype _ id'] = $ movie_type)
{
Echo '<option value = "'. $ row ["movietype_id"]. '"selected =" selected "> '. $ row ["movietype_label"]. '</option> ';
}
Else
{
Echo '<option value = "'. $ row [" movietype_id "]. '">'. $ row ["movietype_label"]. '</option> ';
}
}
?>
</Select> </td>
</Tr>
<Tr>
<Td> Movie Year </td>
<Td> <select name = "movie_year" id = "">
<? Php
For ($ yr = date ('y'); $ yr> 1970; $ yr --)
{
If ($ yr = $ movie_year)
{
Echo '<option value = "'. $ yr. '" selected = "selected">'. $ yr. '</option> ';
}
Else
{
Echo '<option value = "'. $ yr. '">'. $ yr. '</option> ';
}
}
?>
</Select> </td>
</Tr>
<Tr>
<Td> Lead actor </td>
<Td> <select name = "movie_leadactor" id = "">
<? Php
$ Query = 'select people_id, people_fullname from people where people_isactor = 1 order by lele_fullname ';
$ Result = mysql_query ($ query, $ db) or die (mysql_error ($ db ));
While ($ row = mysql_fetch_assoc ($ result ))
{
If ($ row ["people_id"] = $ movie_leadactor)
{
Echo '<option value = "'. $ row ["lele_id"]. '"selected =" selected "> '. $ row ["lele_fullname"]. '</option> ';
}
Else
{
Echo '<option value = "'. $ row [" people_id "]. '">'. $ row ["lele_fullname"]. '</option> ';
}
}
?>
</Select> </td>
</Tr>
<Tr>
<Td> Director </td>
<Td> <select name = "movie_director" id = "">
<? Php
$ Query = 'select * from people where people_isdirector = 1 order by lele_fullname ';
$ Result = mysql_query ($ query, $ db) or die (mysql_error ($ db ));
While ($ row = mysql_fetch_assoc ($ result ))
{
If ($ row ['people _ id'] = $ movie_director)
{
Echo '<option value = "'. $ row ['people _ id']. '"selected =" selected "> '. $ row ["lele_fullname"]. '</option> ';
}
Else
{
Echo '<option value = "'. $ row ['people _ id']. '">'. $ row ["lele_fullname"]. '</option> ';
}
}
?>
</Select> </td>
</Tr>
<Tr>
<Td colspan = "2" style = "text-align: center">
<? Php
If ('edit' = $ _ GET ['action'])
{
Echo '<input type = "hidden" value = "'. $ _ GET [" id "]. '" name = "movie_id"/> ';
}
?>
<Input type = "submit" name = "submit" value = "<? Php echo ucfirst ($ _ GET ['action']);?> "/>
</Td>
</Tr>
</Table>
</Form>
</Body>
</Html>
Copy code
Note that the red statement can be used directly after extract is used. The variable name is a field name and the variable value is a field value, this is somewhat similar to the method Movie in ado.net to retrieve data from DataReader or DataSet. name = DataSet. table [o]. row [I] ["Name"]. toString () Movie. name = reader ["Name"]. toString ();