在php中的每个字符串中转义单引号

I have a problem like this one:

Write a function that takes an argument that can be array of strings or arrays. The function should escape quotes in every string it find and return the modified array.

Now my solution is:

function string(arr){

      foreach ($arr as $key => $value) {
        $newValue = str_replace(" ", '', $arr[$key]);
        return $arr; 
      }  
 }

My questions is am I doing right? Is my solution is right also? Am I understanding it correctly? Based on my understanding, I should replace onyl all the single quotes find in the string. Any suggestions is much appreciated. Thank you very much

Well...no, you're not really on the right track.

Here's what I propose.

If you're using PHP 5.3+:

function escapeQuotes(array $array)
{
     $return_array = [];
     array_walk_recursive($array, function($x) use (&$return_array)
     {
         $return_array[] = str_replace("'", "\\'", $x); 
         // note that this will escape the single quote not replace it. 
         // Not sure on the \\ behaviour though.
         // Things may get weird when returned
     }
     return $return_array;

}

You need something like this

 <?php
  function string_convert($str_or_arr){

        if(is_array($str_or_arr)){
            $new_value = array();
            foreach($str_or_arr as $str_val){
                $new_value[] = string_convert($str_val);
            }
            return $new_value[];
        } else {
             return str_replace("'","",$str_or_arr);
        }
     }
 ?>