数据表行选择 - PHP 5.2 上的匿名函数


Datatables row selection - Anonymous function on PHP 5.2

我正在使用这个数据表示例,在我的服务器上使用PHP版本5.2。

我知道不能使用匿名函数,例如第 35 行示例中的"服务器端脚本",但我需要某种解决方案,因为我无法升级我的服务器。

这就是问题所在:

array( 'db' => 'id', 'dt' => 'DT_RowId', 'formatter' => function( $d, $row ) { // Technically a DOM id cannot start with an integer, so we prefix // a string. This can also be useful if you have multiple tables // to ensure that the id is unique with a different prefix return 'row_'.$d; }),

有没有人有针对该function($d, $row)的解决方案?

'也许你可以使用array_map:

array_map("function", array('db' => 'id', ...((,

然后定义函数以对"格式化程序"元素执行您想要的操作,但保留所有其他元素。

我将尝试详细说明,这是建议的代码(我假设只有$d是整数,如果不是,您可能可以使用另一个识别器代替 is_int(((:

array_map(
  function($val) {$val = is_int($val) ? 'row_'.$d : $val}, 
  array('db' => 'id', 'dt' => 'DT_RowId','formatter' => $d)
)

根据您使用的 php 版本,您可能需要定义一个非匿名函数。

这是一个应该在 5.2 中工作的工作示例:

<?php
  $d = 10;
  function func($val) {
    return is_int($val) ? 'row_'.$val : $val;}
  print_r(array_map(     
    "func", 
    array('db' => 'id', 'dt' => 'DT_RowId','formatter' => $d)
   ));
?>

这输出:

Array ( [db] => id [dt] => DT_RowId [formatter] => row_10 )