Skip to content

Instantly share code, notes, and snippets.

@huzemin
Created January 25, 2016 06:18
Show Gist options
  • Save huzemin/c7fd863da36af1703c12 to your computer and use it in GitHub Desktop.
Save huzemin/c7fd863da36af1703c12 to your computer and use it in GitHub Desktop.
获取数组中特定键的数据,并进行默认值设置
<?php
/**
* @author 胡泽民 <[email protected]>
*/
// 获取数组中特定键的数据,并进行默认值设置
function filter_array($params, $keys) {
if(!is_array($params)) {
return null;
}
if(!is_array($keys)) {
$keys = explode(',', $keys);
$_keys = array();
foreach($keys as $k => $v) {
$v = explode('#', $v);
if(count($v) == 2) {
$_keys[trim($v[0])] = trim($v[1]);
} else {
$_keys[trim($v[0])] = null;
}
}
$keys = $_keys;
unset($_keys);
}
if(count($keys) > 0) {
$_params = array();
foreach($keys as $k => $v) {
if(isset($params[$k])) {
$_params[$k] = $params[$k];
} else {
$_params[$k] = $v;
}
}
return $_params;
}
return $params;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment