extract


php128 apg

IMPORT variables into the current symbol table from an ARRAY.





This function, This function treats keys as variable names and values as variable values.

For each key/value pair it will create a variable in the current symbol table, subject to $flags and $prefix parameters.

We must use an associative ARRAY; a numerically indexed ARRAY will not produce results unless you use EXTR_PREFIX_ALL or EXTR_PREFIX_INVALID.

Worth noting that $prefix is only required if $flags is EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS.

If the prefixed result is not a valid variable name, it is not imported into the symbol table.

Prefixes are automatically separated from the array key by an underscore character.



<?php

int extract 
arr &$array [, int $flags EXTR_OVERWRITE [, 
                                          
str $prefix NULL ]] )


where,

&
$array An associative ARRAY
                ( 
PASSED by reference )

$flags To control the way  invalid/numeric keys and
                     
collisions are treated is determined by the extraction
             
SEE the below TABLE )
                
$prefix The prefix to consider or not

?>

&$array


An associative ARRAY where the variables will be imported



 $flags 


The way invalid/numeric keys and collisions are treated is determined by the extraction flags.

It can be one of the following values:

CONSTANT VALUE OBSERVATIONS
EXTR_OVERWRITE

DEFAULT
0 If there is a collision, overwrite the existing variable.
EXTR_SKIP 1 If there is a collision, don't overwrite the existing variable
EXTR_PREFIX_SAME 2 If there is a collision, prefix the variable name with $prefix.
EXTR_PREFIX_ALL 3 Prefix variable names with $prefix.
EXTR_PREFIX_INVALID 4 Only prefix invalid/numeric variable names with $prefix.
EXTR_PREFIX_IF_EXISTS 5 Only create prefixed variable names if the non-prefixed version of the same variable exists in the current symbol table.
EXTR_IF_EXISTS 6 Only overwrite the variable if it already exists in the current symbol table, otherwise do nothing.
This is useful for defining a list of valid variables and then extracting only those variables you have defined out of $_REQUEST, for example.
EXTR_REFS 256 Extracts variables as references.
This effectively means that the values of the imported variables are still referencing the values of the $array parameter.
You can use this flag alone or combine it with any other flag using the $flags signaling.
ed48


 $prefix 


The prefix of the variable to be considered or not.



 WARNING 

Do not use this function on untrusted data, like user input, (i.e. $_GET, $_FILES, etc.).

If you do, for example if you want to temporarily run old code that relied on register_globals, make sure you use one of the non-overwriting $flags values such as EXTR_SKIP and be aware that you should extract in the same order that's defined in variables_order within the php.ini.

If you still have register_globals and it is turned on, if you use this function on $_FILES and specify EXTR_SKIP, you may be surprised at the results.

This is not recommended practice and is only documented here for for didactic reasons.

The use of register_globals is deprecated.

A possible use for this function is to import into the symbol table variables contained in an associative array returned by the function wddx_deserialize, however, this will be removed in PHP 7.4.0.



  1 EXERCISE   

<?php

$colorblue 
"AERO BLUE";
$colorgreen "AVOCADO";
$colorred"AMERICAN ROSE";

$arr01 = [ "red" => "#FF033E"
            
"green" => "#568203"
              
"blue" => "#C9FFE5" ];

extract($arr01EXTR_OVERWRITE);

echo 
'<pre>';
print_r($arr01);

echo 
'</pre><br><br>' $colorred 
                        
'&nbsp;= &nbsp;' 
                                    
$red '<br><br>';

echo 
$colorgreen 
                        
'&nbsp;=&nbsp;' 
                                    
$green '<br><br>';

echo 
$colorblue 
                        
'&nbsp;=&nbsp;' 
                                    
$blue '<br>';

?> 

  2 EXERCISE   

<?php

$arr0102 
= [ 'f1' => "Apple"
                    
'f2' => "Strawberry"
                    
'f3' => "Pineapple"
                    
'f4' => "Banana"
                    
'f5' => "Orange" ];
                
extract($arr0102EXTR_PREFIX_SAMENULL);
                
echo 
$f1' is red, '$f5' is orange.' '<br>';
                          
?> 

  3 EXERCISE   

<?php

$data03 
'AEAD.txt';
// See the below file

$rdata03 file_get_contents($data03);

echo 
$rdata03 '<br>';

$xdata03 explode('<br>'$rdata03);

extract($_FILES256);
// 256 = EXTR_REFS

echo $xdata03[2] . '<br>';
                         
?> 

AEAD, Autenticated Encryption with 
Associated Data, is a form of encryption which 
simultaneously provides confidentiality, integrity, 
and authenticity assurances on the data.<br><br>GCM, Galois/Counter Mode
 is a mode of operation for symmetric key cryptographic
 block ciphers that has been widely adopted because of 
 its efficiency and performance.<br><br>
GCM throughput rates for state-of-the-art, 
high-speed communication channels can be achieved 
with reasonable hardware resources.<br><br>
CCM, (Counter with CBC-MAC) is a mode of operation 
for cryptographic block ciphers.
It is an authenticated encryption algorithm designed 
to provide both authentication and confidentiality.<br><br>
CCM mode is only defined for block ciphers with a 
block length of 128 bits.<br><br>

  4 EXERCISE   

<?php

$val 
4;
$str "John";

debug_zval_dump($val);
echo 
'<br>'
debug_zval_dump($str);
echo 
'<br><br>'

/* Extracting Global Variables */
var_dump(extract($GLOBALSEXTR_REFS));
echo 
'<br>'
debug_zval_dump($val);
echo 
'<br>'
debug_zval_dump($str);

?>

  5 EXERCISE   

<?php

/* various combinations of arrays
                     to be used for the test */
$mixed_array = array(
array(),
array( 
1,2,3,4,5,6,7,8,),
array( 
"One""Two""Three""Four""Five" ),
);

$counter 0;

foreach ( 
$mixed_array as $sub_array ) {
echo 
"<br><br>Iteration: $counter<br><br>";
$counter++;

var_dump extract($sub_array));
/* Single Argument */
echo '<br><br>';
/* variations of two arguments */
var_dump extract($sub_arrayEXTR_OVERWRITE));
echo 
'<br>';
var_dump extract($sub_arrayEXTR_SKIP));
echo 
'<br>';
var_dump extract($sub_arrayEXTR_IF_EXISTS));
echo 
'<br><br>';
/* variations of three arguments 
          with use of various extract types */
var_dump extract($sub_arrayEXTR_PREFIX_INVALID"ssd"));
echo 
'<br>';
var_dump extract($sub_arrayEXTR_PREFIX_SAME"sss"));
echo 
'<br>';
var_dump extract($sub_arrayEXTR_PREFIX_ALL"bb"));
echo 
'<br>';
var_dump extract($sub_arrayEXTR_PREFIX_ALL""));
// "_" taken as default prefix
echo '<br>';
var_dump extract($sub_arrayEXTR_PREFIX_IF_EXISTS"bb"));
}

?>

  6 EXERCISE   

<?php

/* various combinations of arrays 
               to be used for the test */
$mixed_array = array(
array( 
6"six"7"seven"8"eight"9"nine" ),
array( 
"a" => "aaa""A" => "AAA"
                      
"c" => "ccc""d" => "ddd""e" => "eee" ),
array( 
"1" => "one""2" => "two"
            
"3" => "three""4" => "four""5" => "five" ),
);

$counter 0;

foreach ( 
$mixed_array as $sub_array ) {
echo 
"<br><br>Iteration: $counter<br><br>";
$counter++;

var_dump extract($sub_array));
/* Single Argument */
echo '<br>';
/* variations of two arguments */
var_dump extract($sub_arrayEXTR_OVERWRITE));
echo 
'<br>';
var_dump extract($sub_arrayEXTR_SKIP));
echo 
'<br>';
var_dump extract($sub_arrayEXTR_IF_EXISTS));
echo 
'<br><br>';

/* variations of three arguments 
           with use of various extract types*/
var_dump extract($sub_arrayEXTR_PREFIX_INVALID"ssd"));
echo 
'<br>';
var_dump extract($sub_arrayEXTR_PREFIX_SAME"sss"));
echo 
'<br>';
var_dump extract($sub_arrayEXTR_PREFIX_ALL"bb"));
echo 
'<br>';
var_dump extract($sub_arrayEXTR_PREFIX_ALL""));
// "_" taken as default prefix
echo '<br>';
var_dump extract($sub_arrayEXTR_PREFIX_IF_EXISTS"bb"));
}

?>

  7 EXERCISE   

<?php

$mixed_array 
= array(
array( 
=> "one"=> "two"=> 7
                     
=> "four"=> "five" ),
array( 
"f" => "fff""1" => "one"=> 6
               
"" => "blank"2.4 => "float""F" => "FFF",
               
"blank" => ""3.7 => 3.7,
               
5.4 => 7=> 8.6'5' => "Five"
               
"4name" => "jonny""a" => NULL
               
NULL => ),
array( 
12"name"'age''45' ),
);

$counter 0;

foreach ( 
$mixed_array as $sub_array ) {
echo 
"<br><br>Iteration: $counter<br><br>";
$counter++;

var_dump extract($sub_array)); 
/* Single Argument */
 
echo '<br>';
/* variations of two arguments */
var_dump extract($sub_arrayEXTR_OVERWRITE));
 echo 
'<br>';
var_dump extract($sub_arrayEXTR_SKIP));
 echo 
'<br>';
var_dump extract($sub_arrayEXTR_IF_EXISTS));
 echo 
'<br><br><br>';

/* variations of three arguments 
             with use of various extract types*/
var_dump extract($sub_arrayEXTR_PREFIX_INVALID"ssd"));
 echo 
'<br>';
var_dump extract($sub_arrayEXTR_PREFIX_SAME"sss"));
 echo 
'<br>';
var_dump extract($sub_arrayEXTR_PREFIX_ALL"bb"));
 echo 
'<br>';
var_dump extract($sub_arrayEXTR_PREFIX_ALL""));
// "_" taken as default prefix
 
echo '<br>';
var_dump extract($sub_arrayEXTR_PREFIX_IF_EXISTS"bb"));
}

?>

  8 EXERCISE   

<?php

$mixed_array 
= array(
  array( array(
"oNe""tWo"4), 
  array(
1020304050), array() ),
  array( 
"one" => 1"one" => 2"three" => 3
                
34=> 33=> 4456,
               
5.4 => 545.7 => 57
               
"5.4" => 554"5.7" => 557 )
);

$counter 0;

foreach ( 
$mixed_array as $sub_array ) {
  echo 
"<br><br>Iteration: $counter<br><br>";
  
$counter++;

  
var_dump extract($sub_array));
  
/* Single Argument */
  
echo '<br>';
  
/* variations of two arguments */
  
var_dump extract($sub_arrayEXTR_OVERWRITE));
  echo 
'<br>';
  
var_dump extract($sub_arrayEXTR_SKIP));
  echo 
'<br>';
  
var_dump extract($sub_arrayEXTR_IF_EXISTS));
  echo 
'<br><br><br>';

/* variations of three arguments 
              with use of various extract types*/
var_dump extract($sub_arrayEXTR_PREFIX_INVALID"ssd"));
echo 
'<br>';
var_dump extract($sub_arrayEXTR_PREFIX_SAME"sss"));
echo 
'<br>';
var_dump extract($sub_arrayEXTR_PREFIX_ALL"bb"));
echo 
'<br>';
var_dump extract($sub_arrayEXTR_PREFIX_ALL""));
// "_" taken as default prefix
echo '<br>';
var_dump extract($sub_arrayEXTR_PREFIX_IF_EXISTS"bb"));
}

?>

  9 EXERCISE   

<?php

/* EXTR_REFS as second Argument */
$a = array ('foo' => 'aaa');

echo 
'The given $a:<br>';
var_dump($a);

echo 
'<br><br>After extract($a, EXTR_REFS):<br>';          
var_dump extract($aEXTR_REFS));
echo 
'<br><br>$foo:<br>';
var_dump($foo);

$b $a;
$b['foo'] = 'bbb';
echo 
'<br><br>$b:<br>';
var_dump($b);

echo 
'<br><br>After extract($a, EXTR_REFS):<br>';
var_dump extract($aEXTR_REFS));
echo 
'<br><br>$foo:<br>';
var_dump($foo);
echo 
'<br><br>$a:<br>';
var_dump($a);

?>

  10 EXERCISE   

<?php

/* EXTR_PREFIX_ALL called twice with same prefix string */
echo "Testing for 
               EXTR_PREFIX_ALL 
                   called twice with same prefix string.<br><br>"
;
                   
$a = array( "1" => "one"
                  
"2" => "two"
                  
"3" => "three"
                  
"4" => "four"
                  
"5" => "five" );
                  
echo 
'The given $a:<br>';
var_dump($a);

echo 
'<br><br>After extract($a, EXTR_PREFIX_ALL, "same"):<br>';  
var_dump extract($aEXTR_PREFIX_ALL"same"));

$b = array( "f" => "fff"
                   
"1" => "one"
                    
=> 6
                   
"" => "blank"
                  
2.4 => "float"
                   
"F" => "FFF"
            
"blank" => ""
                   
3.7 => 3.7
                   
5.4 => 7
                   
=> 8.6
                  
'5' => "Five"
        
"4name" => "jonny"
                
"a" => NULL
            
NULL => );
            
echo 
'<br><br>The given $b:<br>';
var_dump($b);

echo 
'<br><br>After extract($b, EXTR_PREFIX_ALL, "same"):<br>';                 
var_dump extract($bEXTR_PREFIX_ALL"same"));
echo 
'<br><br>After extract($b, EXTR_PREFIX_ALL, "diff"):<br>';  
var_dump extract($bEXTR_PREFIX_ALL"diff"));

?>

  11 EXERCISE   

<?php

/* To show variables with numerical 
            prefixes cannot be extracted */
$var["i"] = 1;
$var["j"] = 2;
$var["k"] = 3;
echo 
"Testing for Numerical prefixes.<br><br>";
var_dump(extract($var));

$var1["m"] = 1;
$var1[2] = 2;
$var1[] = 3;
var_dump extract($var1));

?>

  12 EXERCISE   

<?php

$a 
= array('foo' => 'original.foo');
$nonref $a['foo'];
$ref = &$a;
extract($aEXTR_REFS);
$a['foo'] = 'changed.foo';
var_dump($nonref);

?>

  13 EXERCISE   

<?php

$a 
= array('foo' => 'original.foo');
$ref = &$a;
$foo 'test';
extract($aEXTR_OVERWRITE|EXTR_REFS);
$foo 'changed.foo';
var_dump($a['foo']);

?>