sfsefsf33fs3fs3fs
sfsefsf33fs3fs3fs

Reputation: 655

PHP functions array

I'm trying to get an array() inside of a function in PHP.

$CheckInfo->gatherInfo(array("hej", "ds"), "email");

And then collect it as:

public function checkSecurity($input, $type){

            // Set new varibels
            $input = htmlentities(addslashes($input));
            $type = htmlentities(addslashes($type));

            $this->findPath($input, $type);

        }

But once I do use htmlentities(addslashes)) if gives me this error: Warning: addslashes() expects parameter 1 to be string, array given in

Without addslashes and htmlentities, it gives me a return on "array". How may I use a array, read it and use it in a function?

Upvotes: 0

Views: 161

Answers (1)

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

You can use array_map() function for this. array_map() applies the callback function to each element of the array.

private function sanitize_elements($element){
    return htmlentities(addslashes($element));
}

public function checkSecurity($input, $type){

    $input = array_map(array($this, 'sanitize_elements'), $input);
    $type = htmlentities(addslashes($type));
    $this->findPath($input, $type);

}

So it will send each value of the array to sanitize_elements() method, apply htmlentities() and addslashes() function to each value, and return an array with the new values.

Here's the reference:

Upvotes: 1

Related Questions