0

I'm using AppServ 2.6.0 / Apache 2.2.8 / PHP 6.0.0-dev, and I'm getting an error with the following function.

<? if(count($ex) > 0) {
    foreach($ex as $k => $v) { 
      echo "<tr><td style='font-size:12px;'>".strip_tags($k)."</td>
      <td style='font-size:10px;'>".$v['count']."</td>
      <td style='font-size:10px;'>".implode(", ", array_map(function ($k, $v)
          { return $k."/".$v; }, array_keys($v['players']), array_values($v['players'])))."
      </td></tr>";  
    } 
 } ?>

The error is (Line 14 is where the array_map() is):

Parse error: syntax error, unexpected T_FUNCTION, expecting ')' ** on line 14

Sadly, I can't change the PHP version (I'm aware that the error might have to do with the PHP version). If I upgrade PHP, the whole project, that wasn't started by me, would fall apart.

Is there any way to get this code working on Apache 2.2.8 / PHP 6.0.0-dev?

If there isn't any way around this, and I'm forced to change my php version, is there any version out there that has PHP5.3 anonymous functions that also keeps the deprecated ones?

Jongware
  • 22,200
  • 8
  • 54
  • 100
danks
  • 103
  • 1
  • 10
  • Make it an ordinary predeclared named function? – mario Jan 12 '14 at 13:48
  • 2
    "...that also keeps the deprecated ones?" - What does that mean exactly? I also find it a bad idea to use PHP 6.0.0-dev, which is an alpha version of something that doesn't exist anymore. You should definitely switch to a "real" version of PHP either way. – deceze Jan 12 '14 at 13:51
  • You may use [create_function](http://php.net/create_function) which is available since PHP 4.0.1. –  Jan 12 '14 at 13:55

1 Answers1

2

Instead of using anonymous function you can define it as a "regular" function and pass its name to array_map:

function foo($k, $v) {
    return $k."/".$v;
}

array_map('foo', $my_array);
lafor
  • 12,472
  • 4
  • 32
  • 35
  • 1
    Well, this definitely made me feel stupid, I spent the last hour trying to do something about this and it never occurred me to do this. Thanks a lot. – danks Jan 12 '14 at 13:54
  • @danks When your question is answered, rather than leaving a comment thanking the answerer, please mark the answer as accepted. Once you have 15 rep, you can also upvote the accepted answer and any others you found helpful. Please see http://stackoverflow.com/help/someone-answers – Adi Inbar Jan 13 '14 at 03:23