Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * The Much easier way is to use array_diff_key
- * 用数组的键过滤数关联组
- * @param array $arr ['k1' => 'va1', 'k2' => 'va2', 'k3' => 'va3']
- * @param string $keyStr 'k1,k2'
- * @param string $type 'include|exclude'
- * @return unknown[]
- */
- function filter_array_by_key($arr, $keyStr, $type = 'include') {
- $result = [];
- $keys = explode(',', $keyStr);
- if (strtolower($type) == 'include') {
- foreach ($keys as $k) {
- if (isset($arr[$k])) $result[$k] = $arr[$k];
- }
- }
- if (strtolower($type) == 'exclude') {
- foreach ($arr as $k => $v) {
- if (!in_array($k, $keys)) {
- $result[$k] = $v;
- }
- }
- }
- return $result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement