0

I have an array which contains others arrays. How can apply the function to the array without calling every member of each array on the main array ($inputarray)?

$inputarray = array($product, $first_name, $last_name, $email_from, $preftel, $address, $zip, $city, $address_array, $zip_array, $city_array, $per, $datew, $hqty, $nrf, $tw, $meters, $comments);  

function create_safe_array($a){
  global $link;                                 
  return $link->real_escape_string($a);
}

$inputarray = array_map('create_safe_array', $inputarray);

EDIT:

require_once "sql.php";
$link = new mysqli($host, $user, $pw, $db) or die(".....");
$link->set_charset('UTF8');


$inputarray = array($product, $first_name, $last_name, $email_from, $preftel, $address, $zip, $city, $per, $datew, $hqty, $nrf, $tw, $meters, $comments);  

function create_safe_array($a){
  global $link;                                 
  return $link->real_escape_string($a);
}

$inputarray = array_map('create_safe_array', $inputarray);


$dateactial = date('d.m.Y');

$timeactual= date('H:i');

$sql = "INSERT INTO ORD (Date, Time, Prod, Name, LName, Mail, Tel, Address, ZIP, City,  Per, Date_When, Quantity, Nr_fo, Twag, Meters, Osservazioni) VALUES ('$dateactial ', '$timeactual', '$inputarray[0]', '$inputarray[1]', '$inputarray[2]', '$inputarray[3]', '$inputarray[4]', '$inputarray[5]', '$inputarray[6]', '$inputarray[7]', '$inputarray[8]', '$inputarray[9]', '$inputarray[10]', '$inputarray[11]', '$inputarray[12]', '$inputarray[13]', '$inputarray[14]')";

$link->query($sql) or die("....");
$link->close();
2
  • 3
    since it would appear that you're using the mysqli library, you might want to consider using prepared statements for your queries rather than escaping the variables. Commented May 12, 2013 at 21:04
  • As of PHP 5.3 you should use anonymous function for implementing one-time callbacks. Commented May 12, 2013 at 21:17

2 Answers 2

6

Do it recursively:

function create_safe_array($a){
  if(is_array($a)){
     return array_map('create_safe_array', $a);
  }
  global $link;                                 
  return $link->real_escape_string($a);
}
Sign up to request clarification or add additional context in comments.

Comments

4

You can use array_walk_recursive() instead of array_map (remembering that the array is passed by reference); and if you're on PHP 5.3.x or above, you can pas link using use rather than global.

array_walk_recursive(
    $inputarray,
    function (&$value) use ($link) {
        $value = $link->real_escape_string($value);

    }
);

but (as Spudley has commented) prepared statements are a cleaner, safer option when working with MySQLi

2 Comments

Thank you for the reply. As I never used prepared statements, can you take me a complete example of a prepared statements for this case? Thank you
Without knowing your SQL, it's rather difficult to show you an example of a prepared statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.