0

I am working on a big script which will generate some string or array or multidimensional array i want use mysql_real_escape_string for all array / string for that this i tried the below code

function check($data) {
    if(!is_array($data)) {
        return mysql_real_escape_string($data);
    } else if (is_array($data)) {
       $newData = array();
       foreach($data as $dataKey => $dataValue){
             if(!is_array($dataValue)){
                 $key = mysql_real_escape_string($dataKey);
                 $value = mysql_real_escape_string($dataValue);
                 $newData[$key] = $value;
             }
        }
        return $newData;
     }  
}

if i use like this check('saveme'); this returns value

if i pass a array it returns corrent value [ check(array('a','b','c',1,2,3)) ]

if i pass multidimensional array i get [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 i want to use mysql_real_escape_string for array key too.

3
  • 1
    What is purpose of this function? Commented Jun 27, 2014 at 8:41
  • make recursive function Commented Jun 27, 2014 at 8:41
  • (makes an Obi-Wan gesture) you don't need this function. Commented Jun 27, 2014 at 8:58

2 Answers 2

1

You can use array_walk_recursive function, to go throw all leaves of the array, and escape values:

array_walk_recursive($array, function(&$leaf) {
    if (is_string($leaf))
       $leaf = mysql_real_escape_string($leaf);
});

Also, it is good to follow data consistency rules, and do not use !is_array(), but is_string(), because mysql_real_escape_string takes string params, not !string.

Unfortunately, array_walk_recursive is designed so, that it can't edit keys. If you need edit keys, you may want to write your own recursive function. I don't want to copy answer, you can find it here

Sign up to request clarification or add additional context in comments.

3 Comments

@S.Varun do you need only values, or v&k?
@wingofovnia so its not possible right. its possible by custom function
@S.Varun yep. follow the link I've specified. There is your answer.
0

You can use this function :

(MyStringEscapeFunc() is your custom escape function)

function escape_recursive($arr){
    if(is_array($arr)){
        $temp_arr = array();
        foreach ($arr as $key=>$value){
                $temp_arr[MyStringEscapeFunc($key)] = escape_recursive($value);
        }
        return $temp_arr;
    }else{
        return MyStringEscapeFunc($arr);
    }
}

Example Result :

//Before :
Array (
[0] => Array ( 
          [0] => foo'bar
          [1] => bar'baz
        ) 
[1] => foob'ar
[2] => Array ( [foo'baz] => baz'foo ) 
) 



//After : 
Array ( 
[0] => Array (
        [0] => foo\'bar
        [1] => bar\'baz
       )
[1] => foob\'ar 
[2] => Array ( [foo\'baz] => baz\'foo )
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.