<?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
?>
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 |
<?php
$colorblue = "AERO BLUE";
$colorgreen = "AVOCADO";
$colorred= "AMERICAN ROSE";
$arr01 = [ "red" => "#FF033E",
"green" => "#568203",
"blue" => "#C9FFE5" ];
extract($arr01, EXTR_OVERWRITE);
echo '<pre>';
print_r($arr01);
echo '</pre><br><br>' . $colorred .
' = ' .
$red . '<br><br>';
echo $colorgreen .
' = ' .
$green . '<br><br>';
echo $colorblue .
' = ' .
$blue . '<br>';
?>
<?php
$arr0102 = [ 'f1' => "Apple",
'f2' => "Strawberry",
'f3' => "Pineapple",
'f4' => "Banana",
'f5' => "Orange" ];
extract($arr0102, EXTR_PREFIX_SAME, NULL);
echo $f1, ' is red, ', $f5, ' is orange.' . '<br>';
?>
<?php
$data03 = 'AEAD.txt';
// See the below file
$rdata03 = file_get_contents($data03);
echo $rdata03 . '<br>';
$xdata03 = explode('<br>', $rdata03);
extract($_FILES, 256);
// 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>
<?php
$val = 4;
$str = "John";
debug_zval_dump($val);
echo '<br>';
debug_zval_dump($str);
echo '<br><br>';
/* Extracting Global Variables */
var_dump(extract($GLOBALS, EXTR_REFS));
echo '<br>';
debug_zval_dump($val);
echo '<br>';
debug_zval_dump($str);
?>
<?php
/* various combinations of arrays
to be used for the test */
$mixed_array = array(
array(),
array( 1,2,3,4,5,6,7,8,9 ),
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_array, EXTR_OVERWRITE));
echo '<br>';
var_dump ( extract($sub_array, EXTR_SKIP));
echo '<br>';
var_dump ( extract($sub_array, EXTR_IF_EXISTS));
echo '<br><br>';
/* variations of three arguments
with use of various extract types */
var_dump ( extract($sub_array, EXTR_PREFIX_INVALID, "ssd"));
echo '<br>';
var_dump ( extract($sub_array, EXTR_PREFIX_SAME, "sss"));
echo '<br>';
var_dump ( extract($sub_array, EXTR_PREFIX_ALL, "bb"));
echo '<br>';
var_dump ( extract($sub_array, EXTR_PREFIX_ALL, ""));
// "_" taken as default prefix
echo '<br>';
var_dump ( extract($sub_array, EXTR_PREFIX_IF_EXISTS, "bb"));
}
?>
<?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_array, EXTR_OVERWRITE));
echo '<br>';
var_dump ( extract($sub_array, EXTR_SKIP));
echo '<br>';
var_dump ( extract($sub_array, EXTR_IF_EXISTS));
echo '<br><br>';
/* variations of three arguments
with use of various extract types*/
var_dump ( extract($sub_array, EXTR_PREFIX_INVALID, "ssd"));
echo '<br>';
var_dump ( extract($sub_array, EXTR_PREFIX_SAME, "sss"));
echo '<br>';
var_dump ( extract($sub_array, EXTR_PREFIX_ALL, "bb"));
echo '<br>';
var_dump ( extract($sub_array, EXTR_PREFIX_ALL, ""));
// "_" taken as default prefix
echo '<br>';
var_dump ( extract($sub_array, EXTR_PREFIX_IF_EXISTS, "bb"));
}
?>
<?php
$mixed_array = array(
array( 1 => "one", 2 => "two", 3 => 7,
4 => "four", 5 => "five" ),
array( "f" => "fff", "1" => "one", 4 => 6,
"" => "blank", 2.4 => "float", "F" => "FFF",
"blank" => "", 3.7 => 3.7,
5.4 => 7, 6 => 8.6, '5' => "Five",
"4name" => "jonny", "a" => NULL,
NULL => 3 ),
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_array, EXTR_OVERWRITE));
echo '<br>';
var_dump ( extract($sub_array, EXTR_SKIP));
echo '<br>';
var_dump ( extract($sub_array, EXTR_IF_EXISTS));
echo '<br><br><br>';
/* variations of three arguments
with use of various extract types*/
var_dump ( extract($sub_array, EXTR_PREFIX_INVALID, "ssd"));
echo '<br>';
var_dump ( extract($sub_array, EXTR_PREFIX_SAME, "sss"));
echo '<br>';
var_dump ( extract($sub_array, EXTR_PREFIX_ALL, "bb"));
echo '<br>';
var_dump ( extract($sub_array, EXTR_PREFIX_ALL, ""));
// "_" taken as default prefix
echo '<br>';
var_dump ( extract($sub_array, EXTR_PREFIX_IF_EXISTS, "bb"));
}
?>
<?php
$mixed_array = array(
array( array("oNe", "tWo", 4),
array(10, 20, 30, 40, 50), array() ),
array( "one" => 1, "one" => 2, "three" => 3,
3, 4, 3 => 33, 4 => 44, 5, 6,
5.4 => 54, 5.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_array, EXTR_OVERWRITE));
echo '<br>';
var_dump ( extract($sub_array, EXTR_SKIP));
echo '<br>';
var_dump ( extract($sub_array, EXTR_IF_EXISTS));
echo '<br><br><br>';
/* variations of three arguments
with use of various extract types*/
var_dump ( extract($sub_array, EXTR_PREFIX_INVALID, "ssd"));
echo '<br>';
var_dump ( extract($sub_array, EXTR_PREFIX_SAME, "sss"));
echo '<br>';
var_dump ( extract($sub_array, EXTR_PREFIX_ALL, "bb"));
echo '<br>';
var_dump ( extract($sub_array, EXTR_PREFIX_ALL, ""));
// "_" taken as default prefix
echo '<br>';
var_dump ( extract($sub_array, EXTR_PREFIX_IF_EXISTS, "bb"));
}
?>
<?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($a, EXTR_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($a, EXTR_REFS));
echo '<br><br>$foo:<br>';
var_dump($foo);
echo '<br><br>$a:<br>';
var_dump($a);
?>
<?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($a, EXTR_PREFIX_ALL, "same"));
$b = array( "f" => "fff",
"1" => "one",
4 => 6,
"" => "blank",
2.4 => "float",
"F" => "FFF",
"blank" => "",
3.7 => 3.7,
5.4 => 7,
6 => 8.6,
'5' => "Five",
"4name" => "jonny",
"a" => NULL,
NULL => 3 );
echo '<br><br>The given $b:<br>';
var_dump($b);
echo '<br><br>After extract($b, EXTR_PREFIX_ALL, "same"):<br>';
var_dump ( extract($b, EXTR_PREFIX_ALL, "same"));
echo '<br><br>After extract($b, EXTR_PREFIX_ALL, "diff"):<br>';
var_dump ( extract($b, EXTR_PREFIX_ALL, "diff"));
?>
<?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));
?>
<?php
$a = array('foo' => 'original.foo');
$nonref = $a['foo'];
$ref = &$a;
extract($a, EXTR_REFS);
$a['foo'] = 'changed.foo';
var_dump($nonref);
?>
<?php
$a = array('foo' => 'original.foo');
$ref = &$a;
$foo = 'test';
extract($a, EXTR_OVERWRITE|EXTR_REFS);
$foo = 'changed.foo';
var_dump($a['foo']);
?>