Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. /**
  2. * The Much easier way is to use array_diff_key
  3. * 用数组的键过滤数关联组
  4. * @param array $arr ['k1' => 'va1', 'k2' => 'va2', 'k3' => 'va3']
  5. * @param string $keyStr 'k1,k2'
  6. * @param string $type 'include|exclude'
  7. * @return unknown[]
  8. */
  9. function filter_array_by_key($arr, $keyStr, $type = 'include') {
  10. $result = [];
  11. $keys = explode(',', $keyStr);
  12. if (strtolower($type) == 'include') {
  13. foreach ($keys as $k) {
  14. if (isset($arr[$k])) $result[$k] = $arr[$k];
  15. }
  16. }
  17.  
  18. if (strtolower($type) == 'exclude') {
  19. foreach ($arr as $k => $v) {
  20. if (!in_array($k, $keys)) {
  21. $result[$k] = $v;
  22. }
  23. }
  24. }
  25.  
  26. return $result;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement