note 50955 added to function.extract

From: Date: Mon, 14 Mar 2005 22:33:43 +0000
Subject: note 50955 added to function.extract
Groups: php.notes 
Request: Send a blank email to php-notes+get-86440@lists.php.net to get a copy of this message
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); } } } ?> ---- Manual Page -- http://www.php.net/manual/en/function.extract.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+50955 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50955&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50955&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50955&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50955&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50955&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+50955&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#86440) next »