doushuo2834 2015-10-03 03:32
浏览 25
已采纳

升级PHP时匿名函数中断

The following code is suddenly breaking when I switched servers/upgrading from PHP 5.3 to 5.4:

function arrayValRecursive($key, array $arr, $string=false){
    $val = array();
    array_walk_recursive($arr, function($v, $k) use($key, &$val){
        if($k == $key) array_push($val, $v);
    });
    if($string==true){
      return count($val) > 1 ? $val : array_pop($val);
    }
    else {
      return $val;
    }
}

I'm receiving a Parse error: syntax error, unexpected T_FUNCTION error, which seems to be due to the anonymous function in the array_walk_recursive line.

How could I write this function differently to avoid this issue, and why is it happening when upgrading PHP?

Thanks

  • 写回答

1条回答 默认 最新

  • duan_88598 2015-10-03 04:00
    关注

    You are currently using php 5.2 from what I can tell.

    Running a phpinfo() with the code <? echo phpinfo(): ?> would detect the version. From my tests using php 5.2 - 5.5 this only occurs in php 5.2 before lambda functions existed.

    Of course you already know this from our comments, this is for future visitors.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?