0

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

John
  • 11,985
  • 3
  • 45
  • 60
  • looks like array_walk_recursive function still exists in php5.4 http://php.net/manual/en/function.array-walk-recursive.php. there is no point of breaking it.But as I can see you have added anonymous function .. `function ($v,$k)....` I think it needs to modify – Exception Oct 03 '15 at 03:36
  • @Exception that is what I don't understand. The same code runs fine on the 5.3 server and breaks on the 5.4 one. At this point I'm happy for any workaround just to get it running – John Oct 03 '15 at 03:39
  • I got this code snippet from somewhere else a long time ago, and am not familiar with anonymous functions myself, which is why I can't figure out an alternate method to get the same result (assuming one exists) – John Oct 03 '15 at 03:42
  • You must be on 5.2. I cant replicate this on my servers atm. Can you run a phpinfo and confirm version please? – Jesse Oct 03 '15 at 03:48
  • Unable to replicate using a sandbox either. So see if you can get the __specific__ version so I can duplicate it exactly – Jesse Oct 03 '15 at 03:52
  • @Jesse you are correct it is 5.2. My cPanel is lying to me and telling me it was 5.4 - which I'd expect with a new hosting package. phpversion() comes up with 5.2. I'll see to upgrading this. Thanks for the help to you both – John Oct 03 '15 at 03:57

1 Answers1

1

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.

Jesse
  • 2,790
  • 1
  • 20
  • 36
  • Unable to replicate on any of my servers or in any of the online sandboxes that let you change PHP versions, additionally he states in the comments he is in fact on PHP 5.2 after server migration – Jesse Oct 03 '15 at 04:03
  • That is not a great PHP sandbox imho, I purposely avoid that one for bad results. Try http://phptester.net/ or http://sandbox.onlinephpfunctions.com/ – Jesse Oct 03 '15 at 04:05
  • The second one has all the versions... Anyway @Dagon - Additionally it does not break when you execute using your link, the linting functions used to determine syntax errors detects "use" as a string and not part of the lambda. When you run it, you get no errors. This is easily seen when hovering the red X which shows a different error than the OP is asking about – Jesse Oct 03 '15 at 04:08
  • To add to this, when running this on my real servers, where i know the PHP versions, it works like a charm. Cheers – Jesse Oct 03 '15 at 04:09
  • Although cPanel was falsely reporting the PHP version, when I manually switched it to 5.4, it resolved the issue. Thanks for the help. – John Oct 03 '15 at 04:09
  • Cpanel is probably reporting the version of php it is running on, the server could be running multiple php versions. –  Oct 03 '15 at 04:14