Advertisement
thenadz

Sanitize nested array structure

Mar 9th, 2014
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.69 KB | None | 0 0
  1. /**
  2.  * Sanitizes input, converting types in $new to the type of that
  3.  * same value in $old. Any keys not in $old will be stripped out.
  4.  * @param array $new New settings.
  5.  * @param array $old Old settings
  6.  * @return array Sanitized version of $new, based on $old.
  7.  */
  8. private static function sanitizeSettings($new, $old = null) {
  9.    $old = is_null($old) ? self::getSettings() : $old;
  10.    $ret = $old;
  11.  
  12.    foreach ($old as $k => $v) {
  13.       if (isset($new[$k])) {
  14.          if (is_array($v)) {
  15.             $ret[$k] = self::sanitizeSettings((array)$new[$k], $v);
  16.          } elseif (settype($new[$k], gettype($v))) {
  17.             $ret[$k] = $new[$k];
  18.          }
  19.       }
  20.    }
  21.  
  22.    return $ret;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement