drc15469
2014-05-23 14:43
浏览 56
已采纳

filter_var_array() - 未定义的变量

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?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用 filter_var_array() FILTER_CALLBACK 格式化一些数字,我认为这样可行,但它没有:

  $ item_arr = filter_var_array($ item_arr,array(
'item_number'=> array)  (
'过滤器'=> FILTER_CALLBACK,
'选项'=>数组($ this,'number_format')
)
)); 
   
 
  

虽然这确实有效:

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

这两者有什么区别? 为选项分配 array()有什么意义?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyinglan8707 2014-05-23 14:46
    已采纳

    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');
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报