Using mysql_real_escape_string in a multidimensional array in php -
i working on big script generate string or array or multidimensional array want use mysql_real_escape_string
array / string tried below code
function check($data) { if(!is_array($data)) { return mysql_real_escape_string($data); } else if (is_array($data)) { $newdata = array(); foreach($data $datakey => $datavalue){ if(!is_array($datavalue)){ $key = mysql_real_escape_string($datakey); $value = mysql_real_escape_string($datavalue); $newdata[$key] = $value; } } return $newdata; } }
if use check('saveme');
returns value
if pass array returns corrent value [ check(array('a','b','c',1,2,3))
]
if pass multidimensional array [check(array(array('a',array('a','b','c',1,2,3),'c',1,2,3),'b',array('a','b','c',1,2,3),1,2,3))
]
a kind note want use mysql_real_escape_string array key too.
you can use array_walk_recursive function, go throw leaves of array, , escape values:
array_walk_recursive($array, function(&$leaf) { if (is_string($leaf)) $leaf = mysql_real_escape_string($leaf); });
also, follow data consistency rules, , not use !is_array()
, is_string()
, because mysql_real_escape_string
takes string
params, not !string
.
unfortunately, array_walk_recursive
designed so, can't edit keys
. if need edit keys, may want write own recursive function. don't want copy answer, can find here
Comments
Post a Comment