Last Updated: February 25, 2016
·
11.39K
· macodev

Use php array_map in classes

PHP array_map function can be used to map an array with class methods.

Here is an example:

<?php

class Form{

    public function sanitizeData()
    {
        $data = [
            ' this is a text with extra spaces ',
            'and <strong>this</strong> has too<br>much tags'
        ];
        return array_map(array($this, 'sanitize'), $data);
    }

    private function sanitize($value)
    {
        return strip_tags(trim($value));
    }
}

$form = new Form();

var_dump($form->sanitizeData());