note 112849 added to function.filter-input-array
| From: | CertaiN | Date: | Mon, 29 Jul 2013 22:22:54 +0000 |
| Subject: | note 112849 added to function.filter-input-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-195813@lists.php.net to get a copy of this message | ||
This function is very useful for filtering complicated array structure.
Code:
<?php
function filter_request($var, $default_structure) {
$ret = array();
foreach ($default_structure as $key => $value) {
if (!isset($var[$key])) {
$ret[$key] = $value;
} elseif (is_array($value)) {
$ret[$key] = filter_request($var[$key], $value);
} elseif (is_array($var[$key])) {
$ret[$key] = $value;
} else {
$ret[$key] = $var[$key];
}
}
return $ret;
}
?>
Sample Usage:
<?php
$_GET['a']['wrong_structure'] = 'foo';
$_GET['b']['c'] = 'CORRECT';
$_GET['b']['d']['wrong_structure'] = 'bar';
$_GET['unneeded_item'] = 'baz';
var_dump(filter_request($_GET, array(
'a' => 'DEFAULT',
'b' => array(
'c' => 'DEFAULT',
'd' => 'DEFAULT',
),
)));
?>
Sample Result:
array(2) {
["a"]=>
string(21) "DEFAULT"
["b"]=>
array(2) {
["c"]=>
string(12) "CORRECT"
["d"]=>
string(21) "DEFAULT"
}
}
----
Server IP: 69.147.83.199
Probable Submitter: 219.209.194.119
----
Manual Page -- http://php.net/manual/en/function.filter-input-array.php
Edit -- https://master.php.net/note/edit/112849
Del: integrated -- https://master.php.net/note/delete/112849/integrated
Del: useless -- https://master.php.net/note/delete/112849/useless
Del: bad code -- https://master.php.net/note/delete/112849/bad+code
Del: spam -- https://master.php.net/note/delete/112849/spam
Del: non-english -- https://master.php.net/note/delete/112849/non-english
Del: in docs -- https://master.php.net/note/delete/112849/in+docs
Del: other reasons-- https://master.php.net/note/delete/112849
Reject -- https://master.php.net/note/reject/112849
Search -- https://master.php.net/manage/user-notes.php