i made up a mind of how to have a flexible way of initializing needed array indices and filling them with default values if they are missing so i always are safe while accessing those arrays
this is what i came up with
define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
/**
* @desc checks a given array for existance of the keys sepcified and undoes magic_quotes_gpc if neccessary
* @param mixed $what array or single variable to normalize (passed as reference)
* @param mixed $keys array or single arrayindex that is to be checked
* @param mixed $default array or single arrayindex that holds the default value
* @param boolean $trim if true all values get trimmed
* @param boolean $use_associative_keys if set to true $defaults is an associative array
* @return void
*/
function normalize(&$what, $keys, $default = array(), $trim = true, $use_associative_keys = true) {
//remove backslashes added by magiq_quotes_gpc
if (!is_array($what)) {
if (MAGIC_QUOTES_GPC)
$what = stripslashes($what);
return;
}
//make sure $keys and $default are arrays
if (!is_array($keys))
$keys = array($keys);
if (!is_array($default))
$default = array($default);
//process array $keys
foreach($keys as $key => $value) {
if (is_array($value)) {
//it's an array that must be existant
//if array does not exist go create it
if (!isset($what[$key]) or !is_array($what[$key]))
$what[$key] = array();
//recursive call to normalize
$def = $use_associative_keys ? $value : $key;
if (is_array($def))
$def = $key;
$def = isset($default[$def]) ? $default[$def] : array();
normalize($what[$key], $value, $def, $trim, $use_associative_keys);
} elseif (isset($what[$value]) and is_array($what[$value])) {
//it's an index that must be existant and it already holds an array
$def = $use_associative_keys ? $value : $key;
if (is_array($def))
$def = $key;
$def = isset($default[$def]) ? $default[$def] : array();
normalize($what[$value], array_keys($what[$value]), $def, $trim, $use_associative_keys);
} else {
//it's just a simple var
$def = $use_associative_keys ? $value : $key;
if (is_array($def))
$def = $key;
$def = isset($default[$def]) ? $default[$def] : '';
if ($trim)
$what[$value] = isset($what[$value]) ? trim($what[$value]) : trim($def);
else
$what[$value] = isset($what[$value]) ? $what[$value] : $def;
if (MAGIC_QUOTES_GPC)
$what[$value] = stripslashes($what[$value]);
}
}
}
$check = array();
echo '<pre>'.print_r($check, true).'</pre>';
$keys = array('index 1', 'user_id', 'mail');
normalize($check, $keys);
echo '<pre>'.print_r($check, true).'</pre>';
echo '<hr />';
$check = array();
echo '<pre>'.print_r($check, true).'</pre>';
$keys = array('index 1', 'user_id', 'mail');
$defaults['user_id'] = -1;
normalize($check, $keys, $defaults);
echo '<pre>'.print_r($check, true).'</pre>';
echo '<hr />';
$check = array();
echo '<pre>'.print_r($check, true).'</pre>';
$keys = array('index 1', 'user_id', 'mail');
$defaults = array(2, 5, 7);
normalize($check, $keys, $defaults, true, false);
echo '<pre>'.print_r($check, true).'</pre>';
echo '<hr />';
$check = array();
$keys = array('index 1', 'user' => array('id', 'nick'), 'mail');
normalize($check, $keys);
echo '<pre>'.print_r($check, true).'</pre>';
echo '<hr />';
$check = array();
echo '<pre>'.print_r($check, true).'</pre>';
$keys = array('index 1', 'user' => array('id', 'nick'), 'mail');
$defaults['user'] = array('id' => 0, 'nick' => '_not valid_');
normalize($check, $keys, $defaults, true);
echo '<pre>'.print_r($check, true).'</pre>';
output
Array
(
)
Array
(
[index 1] =>
[user_id] =>
[mail] =>
)
---------------
Array
(
)
Array
(
[index 1] =>
[user_id] => -1
[mail] =>
)
---------------
Array
(
)
Array
(
[index 1] => 2
[user_id] => 5
[mail] => 7
)
---------------
Array
(
[index 1] =>
[user] => Array
(
[id] =>
[nick] =>
)
[mail] =>
)
---------------
Array
(
)
Array
(
[index 1] =>
[user] => Array
(
[id] => 0
[nick] => _not valid_
)
[mail] =>
)
what do you think of it?
useful?
overkill?
what to improve?