note 63234 added to function.extract
| From: | nicolaszeh at osu1 dot php dot net | Date: | Thu, 16 Mar 2006 17:24:12 +0000 |
| Subject: | note 63234 added to function.extract | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-105940@lists.php.net to get a copy of this message | ||
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 );
?>
----
Server IP: 217.13.201.10
Probable Submitter: 80.135.191.229
----
X-Spam-Status: No, hits=3.8 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
UPPERCASE_25_50 autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.extract.php
Edit -- http://master.php.net/note/edit/63234
Del: integrated -- http://master.php.net/note/delete/63234/integrated
Del: useless -- http://master.php.net/note/delete/63234/useless
Del: bad code -- http://master.php.net/note/delete/63234/bad+code
Del: spam -- http://master.php.net/note/delete/63234/spam
Del: non-english -- http://master.php.net/note/delete/63234/non-english
Del: in docs -- http://master.php.net/note/delete/63234/in+docs
Del: other reasons-- http://master.php.net/note/delete/63234
Reject -- http://master.php.net/note/reject/63234
Search -- http://master.php.net/manage/user-notes.php