phpdeprecatedphp-7.2

Need help converting deprecated Function create_function() in PHP 7.2


Would anyone be able to update this deprecated piece of code for PHP 7.2. I have found many similar questions and answers, but can't figure out how to convert this particular piece of code.

array_walk($_REQUEST['categories'], create_function('&$c', '$c = "-" . $c;'));

Solution

  • You can use an anonymous function.

    array_walk($_REQUEST['categories'], function(&$c) { $c = "-" . $c; });
    

    Example

    $array = ['a','b','c'];
    array_walk($array, function(&$c) { $c = "-" . $c; });
    print_r($array);
    

    Will produce

    Array
    (
        [0] => -a
        [1] => -b
        [2] => -c
    )