PHP 掩码数组函数 - 用于设置默认数组结构和掩码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 掩码数组函数 - 用于设置默认数组结构和掩码相关的知识,希望对你有一定的参考价值。
/**
* Takes an two arrays and merges them. the first array is used as
* a template or mask, and the second array is applied over top.
* If the second array contains variables or structure not present
* in the first array it is discarded.
*
* The first array acts as a mask on the second array.
*
* @author Robert Hurst
* @website http://thinktankdesign.ca/
*
* @param $defaults
* @param $arguments
* @param bool $keep_unset
* @return array
*/
private function mask_array($defaults, $arguments, $keep_unset = false) {
/*
* If the arguments are invalid or broken fail gracefully by
* returning first argument.
*
* Note: this is done instead of returning false to prevent serious
* failures in other methods or functions that depend on this method.
* This is extremely important in recursive or self executing functions.
*/
if( ! is_array( $defaults ) || ! is_array( $arguments ) ){
return $defaults; //just return the defaults (something goofed)
}
/*
* Copy the default array (the mask) to the results array. If the second
* array is invalid or does not match the mask at all, the default mask
* will be returned.
*/
$results = $defaults;
//loop through the second array
foreach( $arguments as $key => $argument ){
/*
* Check to see if the method is set to discard unmasked data, or if
* the current argument does not fit within the default mask array.
* If both of these are false then discard the variable or structure
*/
if( ! $keep_unset && ! isset( $defaults[$key] ) )
continue; //the option is invalid
/*
* If the current argument is more array structure instead of a
* variable then check if it fits in the default mask. if it does
* re execute on it.
*/
if( is_array( $argument ) ){
/*
* If the mask has a variable instead of structure in this position.
*/
if( !is_array( $defaults[$key] ) ){
/*
* Check to see if the method is set to discard unmasked structure.
* If the method is set to keep unmasked structure then replace the
* mask's variable with this structure.
*/
if( $keep_unset ){
$results[$key] = $argument;
}
//continue to the next item in the second array.
continue;
}
/*
* re execute on the current structure and the current mask position.
*/
$results[$key] = mask_array($defaults[$key], $argument, $keep_unset);
/*
* If the current argument is a variable then save it to the results. Make
* sure the mask does not specify structure.
*/
} else {
/*
* If the mask contains structure at this position then skip to the next
* variable.
*/
if( is_array( $defaults[$key] ) ){
continue;
}
// save the current variable to the results array.
$results[$key] = $argument;
}
}
/*
* After processing all of the array structure, return the new filtered
* array.
*/
return $results;
}
以上是关于PHP 掩码数组函数 - 用于设置默认数组结构和掩码的主要内容,如果未能解决你的问题,请参考以下文章