0

I'm trying to use filter_var_array() and FILTER_CALLBACK to format some numbers, I thought this would work, but it does not:

$item_arr = filter_var_array($item_arr, array(
    'item_number'       => array(
        'filter'    => FILTER_CALLBACK,
        'options'   => array($this, 'number_format')
    )
) );

though this does work:

$item_arr = filter_var_array($item_arr, array(
    'item_number'       => array(
        'filter'    => FILTER_CALLBACK,
        'options'   => function( $num ){
            return number_format( $num );
        }
    )
) );

What's the difference between these two? What's the point of assigning an array() to options?

1 Answer 1

2

In the first example you are trying to create a callback for $this->number_format, but I guess you want the global function number_format instead. If you passing a function (unlike an object method) as callback just the function name as a string should getting passed, like this:

$item_arr = filter_var_array($item_arr, array(
    'item_number'       => array(
        'filter'    => FILTER_CALLBACK,
        'options'   => 'number_format'
    )
));

Check the documentation page about callbacks to get more information.


If you want to format an array of numbers, the function array_walk() seems fitting better:

array_walk($item_arr, 'number_format');
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.