-2

Possible Duplicate:
Parse error: syntax error, unexpected T_FUNCTION line 10 ? help?
Reference - What does this error mean in PHP?

This is the code which cause the error.

$remaining = array_filter($allmodels, function ($var) use ($existmodels) {
                return !in_array($var, $existmodels);
        });

Logic of the code is(below all are arrays)

$remaining = $allmodels - $existmodels;

I think My PHP version is outdated in the server. Is it the problem. How can I create a similar code snippet ?

Thanks

Community
  • 1
  • 1
Techie
  • 44,706
  • 42
  • 157
  • 243

3 Answers3

3

Note: of course global isn't a good pratice

function fil($var) {
 global $existmodels;
 return !in_array($var, $existmodels);
}

$remaining = array_filter($allmodels, 'fil');
dynamic
  • 46,985
  • 55
  • 154
  • 231
2

I am a big fan of OO programming, so just for the fun:

class MyArrayOperations
{
  private $base;

  public function __construct(array $base)
  {
    $this->base = $base;
  }

  public function dif (array $vars ) 
  {
    $result = array();
    foreach ( $this->base as $base )
    if(!in_array($base, $vars))
      $result[] = $base;
    return $result;
  }

$result = (new MyArrayOperations($allmodels))->dif($existmodels);

The class can be put in a separate file for reuse, and then just use the oneliner. And, of course the class can be extended with a kind of handy array operations.

Update

I realized that the oneliner only will work in php 5.4+, so for older versions use this:

$arrayops = new MyArrayOperations($allmodels);
$result = $arrayops->dif($existmodels);

Result in Codepad.

JvdBerg
  • 21,777
  • 8
  • 38
  • 55
0

adding below code to htaccess solved the problem

# Use PHP 5.3
AddType application/x-httpd-php53 .php
Techie
  • 44,706
  • 42
  • 157
  • 243