daily pastebin goal
64%
SHARE
TWEET

Untitled

a guest Mar 25th, 2019 42 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top