0

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?

2
  • 1
    Seems like you are using functions that are used on strings instead of arrays. You can loop over all elements in the array with a foreach-loop, and use those functions on each element in the array. If you try to treat an array as a string, it will output Array. Commented Dec 25, 2015 at 21:40
  • What findPath expects? An array or a string? Commented Dec 26, 2015 at 0:50

1 Answer 1

1

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:

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

Comments

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.