extract

(PHP 3 >= 3.0.7, PHP 4, PHP 5)

extract --  Импортировать переменные из массива в текущую символьную таблицу.

Описание

int extract ( array var_array [, int extract_type [, string prefix]] )

Эта функция используется для импорта переменных из массива в текущую символьную таблицу. Она берёт ассоциативный массив в качестве параметра var_array и трактует его ключи как имена переменных и значения как значения переменных. Для каждой пары ключ/значение будет создана переменная в текущей символьной таблице, согласно параметрам extract_type и prefix.

Замечание: Начиная с версии 4.0.5, эта функция возвращает количество извлечённых переменных.

Замечание: EXTR_IF_EXISTS и EXTR_PREFIX_IF_EXISTS были добавлены в версии 4.2.0.

Замечание: EXTR_REFS был добавлен в версии 4.3.0.

extract() проверяет каждый ключ на соответствие приемлемости в качестве имени переменной. Эта функция также обрабатывает коллизии имён, если ключ совпадает с именем переменной, уже существующей в текущей символьной таблице. Способ обработки неприемлемых/числовых ключей и коллизий имён устанавливает параметр extract_type. Он может принимать одно из следующих значений:

EXTR_OVERWRITE

Если переменная с таким именем существует, она будет переназначена.

EXTR_SKIP

Если переменная с таким именем существует, будет сохранено её прежнее значение.

EXTR_PREFIX_SAME

Если переменная с таким именем существует, к её имени будет добавлен префикс, определённый параметром prefix.

EXTR_PREFIX_ALL

Добавить префикс prefix ко всем именам переменных. Начиная с PHP 4.0.5, также включает числовые имена.

EXTR_PREFIX_INVALID

Добавить префикс prefix только к неприемлемым/числовым именам переменных. Этот флаг был добавлен в PHP 4.0.5.

EXTR_IF_EXISTS

Переназначить только переменные, уже имеющиеся в текущей символьной таблице, в противном случае ничего не делать. Данная возможность полезна для определения списка приемлемых переменных и для извлечения только тех переменных, которые вы уже определили из массивов типа $_REQUEST, например. Этот флаг был добавлен в PHP 4.2.0.

EXTR_PREFIX_IF_EXISTS

Создать переменные, имена которых начинаются с prefix только если переменная с названием, которому не предшествует prefix, определена в текущей символьной таблице. Этот флаг был добавлен в PHP 4.2.0.

EXTR_REFS

Извлечь переменные как ссылки. Это означает, что значения таких переменных будут всё ещё ссылаться на значения массива var_array. Вы можете использовать этот флаг отдельно или комбинировать его с другими, используя OR. Этот флаг был добавлен в PHP 4.3.0.

Если extract_type не определён, он трактуется как EXTR_OVERWRITE.

Обратите внимание, что prefix имеет значение, только если extract_type установлен в EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID или EXTR_PREFIX_IF_EXISTS. Если в результате добавления префикса, не будет получено допустимое имя для переменной, она не будет импортирована в текущую символьную таблицу.

extract() возвращает количество переменных, успешно импортированных в символьную таблицу.

Внимание

Не используйте функцию extract() для непроверенных данных, таких как пользовательский ввод ($_GET, ...). Если вы сделаете это, например, для того, что бы временно запустить старый код, использующий register_globals, используйте соответствующий флаг extract_type для того, что бы не переназначить уже установленные переменные, такой как EXTR_SKIP и удостоверьтесь, что вы извлекаете содержимое массивов $_SERVER, $_SESSION, $_COOKIE, $_POST и $_GET в этом порядке.

Функцию extract() также можно использовать для импорта в текущую символьную таблицу переменных, содержащихся в ассоциативном массиве, возвращённом функцией wddx_deserialize().

Пример 1. Пример использования extract()

<?php

/* Предположим, что $var_array - это массив, полученный в результате
   wddx_deserialize */

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

echo
"$color, $size, $shape, $wddx_size
"
;

?>

Результат выполнения данного примера:

blue, large, sphere, medium

Переменная $size не была переназначена, потому что мы определили EXTR_PREFIX_SAME, в результате чего была создана переменная $wddx_size. Если был бы определён флаг EXTR_SKIP, тогда переменная $wddx_size не была бы создана. EXTR_OVERWRITE был бы причиной того, что переменной $size было бы присвоено значение "medium", и EXTR_PREFIX_ALL был бы причиной того, что были бы также созданы новые переменные $wddx_color, $wddx_size и $wddx_shape.

Вы должны использовать ассоциативный массив, использование числовых массивов не приведёт ни к каким результатам, если вы не использууете EXTR_PREFIX_ALL и EXTR_PREFIX_INVALID.

См. также compact().



extract
nicolas zeh
16-Mar-2006 09:24
This function provides exactly the same functionality as extract except that a parameter was added defining the extract target.
This function can be used if your PHP installation does not support the required Flags or more important if you would like to extract arrays to another destination as to $GLOBALS, i.e. other arrays or objects.
The only difference to extract is that extract_to moves the array pointer of $arr to the end as $arr is passed by reference to support the EXTR_REFS flag.

<?php
  
if( !defined('EXTR_PREFIX_ALL') ) define('EXTR_PREFIX_ALL', 3);
   if( !
defined('EXTR_PREFIX_INVALID') ) define('EXTR_PREFIX_INVALID', 4);
   if( !
defined('EXTR_IF_EXISTS') ) define('EXTR_IF_EXISTS', 5);
   if( !
defined('EXTR_PREFIX_IF_EXISTS') ) define('EXTR_PREFIX_IF_EXISTS', 6);
   if( !
defined('EXTR_REFS') ) define('EXTR_REFS', 256);
  
  
   function
extract_to( &$arr, &$to, $type=EXTR_OVERWRITE, $prefix=false ){
      
       if( !
is_array( $arr ) ) return trigger_error("extract_to(): First argument should be an array", E_USER_WARNING );
      
       if(
is_array( $to ) ) $t=0;
       else if(
is_object( $to ) ) $t=1;
       else return
trigger_error("extract_to(): Second argument should be an array or object", E_USER_WARNING );
      
       if(
$type==EXTR_PREFIX_SAME || $type==EXTR_PREFIX_ALL || $type==EXTR_PREFIX_INVALID || $type==EXTR_PREFIX_IF_EXISTS )
           if(
$prefix===false ) return trigger_error("extract_to(): Prefix expected to be specified", E_USER_WARNING );
           else
$prefix .= '_';
      
      
$i=0;
       foreach(
$arr as $key=>$val ){
          
          
$nkey = $key;
          
$isset = $t==1 ? isset( $to[$key] ) : isset( $to->$key );
          
           if( (
$type==EXTR_SKIP && $isset )
               || (
$type==EXTR_IF_EXISTS && !$isset ) )
                   continue;
          
           else if( (
$type==EXTR_PREFIX_SAME && $isset )
               || (
$type==EXTR_PREFIX_ALL )
               || (
$type==EXTR_PREFIX_INVALID && !preg_match( '#^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$#', $key ) ) )
                  
$nkey = $prefix.$key;
                  
           else if(
$type==EXTR_PREFIX_IF_EXISTS )
               if(
$isset ) $nkey = $prefix.$key;
               else continue;

           if( !
preg_match( '#^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$#', $nkey ) ) continue;
          
           if(
$t==1 )
               if(
$type & EXTR_REFS ) $to->$nkey = &$arr[$key];
               else
$to->$nkey = $val;
           else
               if(
$type & EXTR_REFS ) $to[$nkey] = &$arr[$key];
               else
$to[$nkey] = $val;
          
          
$i++;
       }
      
       return
$i;
   }
  
  
// e.g.:
  
extract_to( $myarray, $myobject, EXTR_IF_EXISTS );
?>
owk dot ch199_ph at gadz dot org
08-Mar-2006 04:21
And if you want with PHP 5 an easy way to extract $V by reference, try this :
<?php
  
foreach ($V as $k => &$v) {
       $
$k =& $v;
   }
?>
It can be used to create special kind of "free args" functions that let you choose when you call them the way you send variables, and which ones. They are moreover very fast to call thanks to references :
<?php
  
function free_args (&$V) {
       foreach (
$V as $k => &$v) {
           $
$k =& $v;
       }
       unset (
$k);  unset ($v);  unset ($V);

      
// be careful that if you need to extract $k, $v or $V variables you should find other names for them in the lines above (ie. $__k, $__v and $__V)
  
}

  
$huge_text = '...';

  
$a = array ('arg1' => 'val1', 'arg2' => &$huge_text); // in this call, only $arg2 will be a true reference in the function
  
free_args ($a);
?>
Be warned that you can't write : "<?php free_args (array ('arg1' => 'val1')); ?>" because the array can't be referenced by the function, as it's not yet created when the function starts.
moslehi<atsign>gmail<d0t>c0m
20-Jan-2006 01:32
Experimentally I found that calling extract() also shows the number of keys if the key is set and is not numeric ! Maybe there was a better definition than mine  . Please have a look to this scripts :

<?PHP
$var
["i"] = "a";
$var["j"] = "b";
$var["k"] = 1;
echo
extract($var); // returns 3
?>

<?PHP
$var2
["i"] = "a";
$var2[2] = "b";
$var2[] = 1;
echo
extract($var2); // returns 1
?>

(Arash Moslehi)
Csaba at alum dot mit dot edu
27-Nov-2005 11:41
Sometimes you may want to extract only a named subset of the key/value pairs in an array.  This keeps things more orderly and could prevent an unrelated variable from getting clobbered from an errant key.  For example,

$things = 'unsaid';
$REQUEST = array(He=>This, said=>1, my=>is, info=>2, had=>a,
                 very=>3, important=>test, things=>4);
$aVarToExtract = array(my, important, info);
extract (array_intersect_key ($REQUEST, array_flip($aVarToExtract)));

will extract
$my = 'is';
$important = 'test';
$info = 2;

but will leave certain
$things = 'unsaid'

Csaba Gabor from Vienna
NB.  Of course the composite request coming in from a web page is in $_REQUEST.
anon at anon dot org
30-May-2005 12:02
A warning about extract() and null values.

This might be an actual Zend2 Engine bug, but it's bad programming practice, so I'm sharing it here instead.

I often work in envrionments where E_STRICT (which would prevent errors like this) isn't on, and I don't have access to change it. I also use a very simple template class that in a nutshell works like this:

$t = new Template('somefile.php');
$t->title = $title;
$t->body = $body;
$t->display();

display() more or less looks like this:

function display(){
   extract(get_object_vars($this),EXTR_REFS);
   ob_start(); include $this->templateFileName;
   return ob_get_clean();
}

If any of the assigned values are null (let's say that in this case $title wasn't initialized above) it causes the engine to do all sorts of incredibly whacky stuff like certifiably lose track of variables in an incredibly inconsistent way. I traced the problem down to the fact that it's using the EXTR_REFS flag. I assume that in PHP's internal variable storage or reference counting mechanism, that trying to extract null references makes it lose track or count of something or rather.

In a nutshell, if you start getting wierd behavior when using extract() make sure that the array or object you are trying to get variables out of doesn't contain null keys or values!
kake26 at gmail dot com
30-Apr-2005 08:59
The following is a neat use for extract to store and manipulate large amounts of form data from. I basically loop through the $_POST and implode it seperating the key and value pairs by a space. Then store it in a db, the reversing function basically explodes the string to a array. Then converts the indexed array to a associative array then uses extract to seal the deal and make it easily available within a program. My main reason for sharing these are the fact I make some big web applications that store allot of forum data in a DB and these functions make it very easy to quickly and easily store and recall the data. I've contributed it because I spent many hours creating this code and recall going "I wish someone had previously submitted it to the page notes". Would have saved me allot of time and agony and I'm sure I'm not the only person that could really benefit from it, so I decided to share.

<?php
$stack
= array();
foreach (
$_POST as $key => $value) {
array_push($stack, $key, $value);
}
// store it
$block = implode(" ",$stack); // yeilds a space delimited string
// insert query to store string in DB here, like the one below
$query = "INSERT INTO `sometable` VALUES('".$seluser."','".addslashes($block)."');";
$result = mysql_query($query) or die("Query failed for block insert: " . mysql_error());
// note $seluser in my case is a user ID associated with that block
// in one of my web apps
?>

The nice thing is with the above we can quickly create a string of key and value pairs from the data the script got. Without really caring what their names are. You know how if register globals are on you say $someformvar rather than $_POST["someformvar"]; , basically the code below reads this previous created block returns it to that state. Sort of like presistant register globals.

<?php
// insert query to grab the previously stored string here
$query = "SELECT * FROM `sometable` WHERE `blockid` = '".addslashes($bid)."';";
$result = mysql_query($query) or die("Query failed read: " . mysql_error());
$sql = mysql_fetch_array($result, MYSQL_ASSOC);
$array = eplode(" ",$sql["data"]);
for (
$i = 0; $i < sizeof($array); $i+=2) {
$myassoc[$array[$i]] = isset($array[$i+1])?$array[$i+1]:NULL;
}
extract($myassoc, EXTR_OVERWRITE);
// now you're key and value pairs from $_POST have been restored
// instead of $_POST
?>
pg dot perfection at gmail dot com
14-Mar-2005 02:33
Here is a little example of how an extraction method should look like when it needs to work recursive (work on nested_arrays too)...

Note that this is only an example, it can be done more easily, and more advanced too.

<?php
/**
 * A nested version of the extract () function.
 *
 * @param    array    $array        The array which to extract the variables from
 * @param    int        $type        The type to use to overwrite (follows the same as extract () on PHP 5.0.3
 * @param    string    $prefix        The prefix to be used for a variable when necessary
 */
function extract_nested (&$array, $type = EXTR_OVERWRITE, $prefix = '')
{
  
/**
     *  Is the array really an array?
     */
  
if (!is_array ($array))
   {
       return
trigger_error ('extract_nested (): First argument should be an array', E_USER_WARNING);
   }

  
/**
     *  If the prefix is set, check if the prefix matches an acceptable regex pattern
     * (the one used for variables)
     */
  
if (!empty ($prefix) && !preg_match ('#^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$#', $prefix))
   {
       return
trigger_error ('extract_nested (): Third argument should start with a letter or an underscore', E_USER_WARNING);
   }

  
/**
     * Check if a prefix is necessary. If so and it is empty return an error.
     */
  
if (($type == EXTR_PREFIX_SAME || $type == EXTR_PREFIX_ALL || $type == EXTR_PREFIX_IF_EXISTS) && empty ($prefix))
   {
       return
trigger_error ('extract_nested (): Prefix expected to be specified', E_USER_WARNING);
   }

  
/**
     * Make sure the prefix is oke
     */
  
$prefix = $prefix . '_';

  
/**
     *  Loop thru the array
     */
  
foreach ($array as $key => $val)
   {
      
/**
         *  If the key isn't an array extract it as we need to do
         */
      
if (!is_array ($array[$key]))
       {
           switch (
$type)
           {
               default:
               case
EXTR_OVERWRITE:
                  
$GLOBALS[$key] = $val;
               break;
               case
EXTR_SKIP:
                  
$GLOBALS[$key] = isset ($GLOBALS[$key]) ? $GLOBALS[$key] : $val;
               break;
               case
EXTR_PREFIX_SAME:
                   if (isset (
$GLOBALS[$key]))
                   {
                      
$GLOBALS[$prefix . $key] = $val;
                   }
                   else
                   {
                      
$GLOBALS[$key] = $val;
                   }
               break;
               case
EXTR_PREFIX_ALL:
                  
$GLOBALS[$prefix . $key] = $val;
               break;
               case
EXTR_PREFIX_INVALID:
                   if (!
preg_match ('#^[a-zA-Z_\x7f-\xff]$#', $key{0}))
                   {
                      
$GLOBALS[$prefix . $key] = $val;
                   }
                   else
                   {
                      
$GLOBALS[$key] = $val;
                   }
               break;
               case
EXTR_IF_EXISTS:
                   if (isset (
$GLOBALS[$key]))
                   {
                      
$GLOBALS[$key] = $val;
                   }
               break;
               case
EXTR_PREFIX_IF_EXISTS:
                   if (isset (
$GLOBALS[$key]))
                   {
                      
$GLOBALS[$prefix . $key] = $val;
                   }
               break;
               case
EXTR_REFS:
                  
$GLOBALS[$key] =& $array[$key];
               break;
           }
       }
      
/**
         *  The key is an array... use the function on that index
         */
      
else
       {
          
extract_nested ($array[$key], $type, $prefix);
       }
   }
}
?>
Michael Newton
02-Mar-2005 05:23
They say "If the result is not a valid variable name, it is not imported into the symbol table."

What they should say is that if _any_ of the results have invalid names, _none_ of the variables get extracted.

Under 4.3.10 on Windows 2000, I was pulling some mySQL records, but needed to convert two fields into IP addresses:
<?
extract
(mysql_fetch_assoc(mysql_query('SELECT * FROM foo')));
extract(mysql_fetch_assoc(mysql_query('SELECT INET_NTOA(bar) AS bar, INET_NTOA(baz) FROM foo')));
?>

I had forgotten the second AS modifier in the SQL query.  Because it couldn't extract a variable called INET_NTOA(baz) into the symbol table, it didn't do either of them.

(BTW I don't normally stack functions up like that!  Just to make a short example!)
21-Feb-2005 07:31
To make this perfectly clear (hopefully), an underscore is always added when the string is prefixed.
extract(array("color" => "blue"),EXTR_PREFIX_ALL,'');// note: prefix is empty
is the same as
$color='_blue';
Aaron Stone
17-Nov-2004 10:44
If you are working porting an older application, and taking the advice above, extracting only _SERVER, _SESSING, _COOKIE, _POST, _GET, you have forgotten to extract _FILES. Putting _FILES last and using EXTR_SKIP doesn't work because the name of the file upload box is already set as a variable containing only the temporary name of the uploaded file from one of the earlier extracts (I haven't tested to see which one specifically, however). A workaround is to put _FILES last and use EXTR_OVERWRITE. This allows extract to replace that temp-name-only variable with the full array of file upload information.
Adam Monsen <adamm at wazamatta dot com>
02-Oct-2004 05:03
As shown in the example, if your 'prefix' is used, a single underscore is added to the name of the extracted variable. Meaning, a prefix of 'p' becomes a prefix of 'p_', so 'blarg' prefixed would be 'p_blarg'.

If you're not sure what variables you've created through extraction, you can call get_defined_vars() to see all defined variables in the current scope.

<endin_array>
 Last updated: Tue, 15 Nov 2005